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]);
}
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]);
}