Pointer Question

P

Patrick

Hi

I am just playing a little bit around with a simple program to use
pointers and unfortunately I get an segmentation violation.
Basically I have 4 arrays that I contain elements that I wanna add
up to produce me a result of 2 elements which should be stored in
another array. My idea was to pass the pointers to the elements to
the add function where I dereference the pointers to add the elements.

Anyone an idea what I am doing wrong?


#include <stdio.h>
#include <stdlib.h>

void add( unsigned long* r_1, unsigned long* r_2,
unsigned long* x_1, unsigned long* x_2,
unsigned long* y_1, unsigned long* y_2 )

{
*r_1 = *x_1 + *x_2;
*r_2 = *y_1 + *y_2;
}

int main( int argc, char* argv[] )
{

unsigned long x_1[] = { 1, 2 };
unsigned long x_2[] = { 3, 4 };

unsigned long y_1[] = { 5, 6 };
unsigned long y_2[] = { 7, 8 };

unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

add(r_1[0],r_2[0],
x_1[0],x_2[0],
y_1[0],y_2[0]);

printf("Y_1[0] = %d, Y_2[0] = %d \n", r_1[0], r_2[0]);

add(r_1[1],r_2[1],
x_1[1],x_2[1],
y_1[1],y_2[1]);

printf("Y_1[1] = %d, Y_2[1] = %d \n", r_1[1], r_2[1]);
}
 
M

Mark Bluemel

Patrick said:
Hi

I am just playing a little bit around with a simple program to use
pointers and unfortunately I get an segmentation violation.
Basically I have 4 arrays that I contain elements that I wanna add
up to produce me a result of 2 elements which should be stored in
another array. My idea was to pass the pointers to the elements to
the add function where I dereference the pointers to add the elements.

Anyone an idea what I am doing wrong?

Yes, naturally :)
#include <stdio.h>
#include <stdlib.h>

void add( unsigned long* r_1, unsigned long* r_2,
unsigned long* x_1, unsigned long* x_2,
unsigned long* y_1, unsigned long* y_2 )
[snip]

unsigned long x_1[] = { 1, 2 };
unsigned long x_2[] = { 3, 4 };

unsigned long y_1[] = { 5, 6 };
unsigned long y_2[] = { 7, 8 };

unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

add(r_1[0],r_2[0],
x_1[0],x_2[0],
y_1[0],y_2[0]);

So your routine "add" is declared to take pointers as arguments and you
pass it longs...

I put your code into a file called "ar.c" and compiled it.

cc ar.c -o ar
ar.c: In function `main':
ar.c:27: warning: passing arg 1 of `add' makes pointer from integer
without a cast
.... repeated for args 2 - 6
ar.c:33: warning: passing arg 1 of `add' makes pointer from integer
without a cast
.... repeated for args 2 - 6

Didn't your compiler give you any errors or warnings?

Add a handful of "&" characters to each call to "add"...
 
P

Patrick

Add a handful of "&" characters to each call to "add"...

Thanks, should I know to be honest.

Anyway, I have found now a nicer way to do it :)

With a macro, and now I dont need the &!

Patrick

#include <stdio.h>
#include <stdlib.h>

#define ADD(r_1, r_2, x_1, x_2, y_1, y_2) \
{ \
r_1 = x_1 + x_2; \
r_2 = y_1 + y_2; \
} \

int main( int argc, char* argv[] )
{

unsigned long x_1[] = { 1, 2 };
unsigned long x_2[] = { 3, 4 };

unsigned long y_1[] = { 5, 6 };
unsigned long y_2[] = { 7, 8 };

unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

ADD(r_1[0],r_2[0],
x_1[0],x_2[0],
y_1[0],y_2[0]);

printf("Y_1[0] = %d, Y_2[0] = %d \n", r_1[0], r_2[0]);

ADD(r_1[1],r_2[1],
x_1[1],x_2[1],
y_1[1],y_2[1]);

printf("Y_1[1] = %d, Y_2[1] = %d \n", r_1[1], r_2[1]);
}
 
D

Default User

Patrick said:
Add a handful of "&" characters to each call to "add"...

Thanks, should I know to be honest.

Anyway, I have found now a nicer way to do it :)

With a macro, and now I dont need the &!

Patrick

#include <stdio.h>
#include <stdlib.h>

#define ADD(r_1, r_2, x_1, x_2, y_1, y_2) \
{ \
r_1 = x_1 + x_2; \
r_2 = y_1 + y_2; \
} \

int main( int argc, char* argv[] )
{

unsigned long x_1[] = { 1, 2 };
unsigned long x_2[] = { 3, 4 };

unsigned long y_1[] = { 5, 6 };
unsigned long y_2[] = { 7, 8 };

unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

ADD(r_1[0],r_2[0],
x_1[0],x_2[0],
y_1[0],y_2[0]);

printf("Y_1[0] = %d, Y_2[0] = %d \n", r_1[0], r_2[0]);

ADD(r_1[1],r_2[1],
x_1[1],x_2[1],
y_1[1],y_2[1]);

printf("Y_1[1] = %d, Y_2[1] = %d \n", r_1[1], r_2[1]);
}


In my opinion, a poor solution. What is the point of even having arrays
if you handle every element individually?

I'd do something like this:

void add(unsigned long* r,
unsigned long* x,
unsigned long* y,
int num_elements)
{
int i;

for (i = 0; i < num_elements; ++i)
{
r = x + y;
}
}

You can avoid the use of the loop control variable i in a couple of
ways if you prefer.



Brian
 
M

Martin Ambuhl

Patrick said:
Hi

I am just playing a little bit around with a simple program to use
pointers and unfortunately I get an segmentation violation.
Basically I have 4 arrays that I contain elements that I wanna add
up to produce me a result of 2 elements which should be stored in
another array. My idea was to pass the pointers to the elements to
the add function where I dereference the pointers to add the elements.

Anyone an idea what I am doing wrong?

You are not passing arguments properly and using incorrect printf
specifiers. You should learn to indent your code and should, even for
C99, return a value from main. Note the changes below:

#include <stdio.h>
#include <stdlib.h>

void add(unsigned long *r_1, unsigned long *r_2,
unsigned long *x_1, unsigned long *x_2,
unsigned long *y_1, unsigned long *y_2)
{
*r_1 = *x_1 + *x_2;
*r_2 = *y_1 + *y_2;
}

int main(void)
{

unsigned long x_1[] = { 1, 2 };
unsigned long x_2[] = { 3, 4 };

unsigned long y_1[] = { 5, 6 };
unsigned long y_2[] = { 7, 8 };

unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

/* mha:added '&' to each argument */
add(&r_1[0], &r_2[0], &x_1[0], &x_2[0], &y_1[0], &y_2[0]);
/* mha: fixed format specifiers */
printf("r_1[0] = %ld, r_2[0] = %ld \n", r_1[0], r_2[0]);

/* mha:added '&' to each argument */
add(&r_1[1], &r_2[1], &x_1[1], &x_2[1], &y_1[1], &y_2[1]);
/* mha: fixed format specifiers */
printf("r_1[1] = %ld, r_2[1] = %ld \n", r_1[1], r_2[1]);
return 0; /* mha: added */
}
 
K

Keith Thompson

Patrick said:
Anyway, I have found now a nicer way to do it :)

With a macro, and now I dont need the &!

Patrick

#include <stdio.h>
#include <stdlib.h>

#define ADD(r_1, r_2, x_1, x_2, y_1, y_2) \
{ \
r_1 = x_1 + x_2; \
r_2 = y_1 + y_2; \
} \
[snip]

Aside from the question of whether a macro is the right approach, the
way you've written your macro can cause problems depending on the
context in which you use it. Read section 10 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, particularly question 10.4.

But in this case, you don't *need* a multi-statement macro, since
there's no control flow. An assignment is an expression; it only
becomes a statement when you add a semicolon. So your ADD macro could
be written as:

#define ADD(r_1, r_2, x_1, x_2, y_1, y_2) \
( (r_1) = (x_1) + (x_2), (r_2) = (y_1) + (y_2) )

The two assignments are expressions, so they're separated by a comma
operator rather than a semicolin. Each reference to argument is
enclosed in parentheses to avoid operator precedence problems.

Again, I'm not suggesting that a macro is the best solution to your
problem (I don't think it is), but *if* you want to use a macro, this
is a better way to do it.
 
P

Peter 'Shaggy' Haywood

Groovy hepcat Martin Ambuhl was jivin' in comp.lang.c on Tue, 24 Jul
2007 3:13 am. It's a cool scene! Dig it.
unsigned long r_1[] = { 0, 0 };
unsigned long r_2[] = { 0, 0 };

/* mha:added '&' to each argument */
add(&r_1[0], &r_2[0], &x_1[0], &x_2[0], &y_1[0], &y_2[0]);
/* mha: fixed format specifiers */
printf("r_1[0] = %ld, r_2[0] = %ld \n", r_1[0], r_2[0]);

I'd hardly call that fixed. Let's try the correct conversion
specifier, just for fun. It's %lu for unsigned long.

printf("r_1[0] = %lu, r_2[0] = %lu \n", r_1[0], r_2[0]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,965
Members
46,294
Latest member
HollieYork

Latest Threads

Top