Thread: Adding arrays with pointers - help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Adding arrays with pointers - help

    I am trying to figure out how to add two arrays. The thing is i have to use pointer notation to add the two to make a third array. I think I am doing this right - but just like all the other times it doesn't work

    Code:
    #include<stdio.h>
    
    int main (void)
    {
    
    	int array1[9];
    	int array2[9];
    	/*int array3[9];*/
    	int i;
    
    	printf ("Please enter ten intergers now.  Seperate each number by a space\n");
    	for ( i = 0; i < 10; i++) {
    		scanf ("%d", &array1[i]);
    	}
    
    	printf ("Please enter ten intergers now.  Seperate each number by a space\n");
    	for ( i = 0; i < 10; i++) {
    		scanf ("%d", &array2[i]);
    	}
    
    	/*int add1and2 (array3[9]);*/
    
    	return 0;
    }
    
    int add1and2 (array1[], array2[], array3[])
    {
    	int array3[9];
    	int *point2a1 = &array1[];
    	int *point2a2 = &array2[];
    	int *point2a3 = &array3[];
    	int i;
    
    	 for(i = 0; i < 10; i++,) {    
    		 array3[i] = *point2a1[i] + *point2a2[i]; 
    	 }
         
    	 return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    So many problems...

    You should really allow for 10 integers since that's what your loops and your prompt calls for:

    int array1[10];
    int array2[10];

    I think you meant for the user to enter all 10 numbers at a time, instead of hitting Enter on each one. So I'd look up how to do scanf for multiple valules, and how to check whether you've gotten all 10.

    Function add1and2 has declaration issues. It should be

    int add1and2(int array1[], int array2[], int array3[])

    Assigning pointers there should be more like

    int *point2a1 = array1;
    int *point2a2 = array2;
    int *point2a3 = array3;

    The loop should be more like

    for(i = 0; i < 10; i++) {
    point2a3[i] = point2a1[i] + point2a2[i];

    But since your exercise calls for "pointer notation" instead of array indexing, it should be transformed into

    Code:
    for(i = 0; i < 10; i++) {    
        *point2a3 = *point2a1 + *point2a2;
        point2a1++;
        point2a2++;
        point2a3++;
        }
    (not checked for syntax)

    And then your main function ought to call add1and2() with the arguments in the right order... then display the results.
    Last edited by nonoob; 10-02-2008 at 06:42 PM.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    it seems your concepts are not clear about pointers, or may be you have copied this code from somewhere else.
    one thing I would suggest you, have well understanding of each line of you code when ever you write any program.
    Code:
    	printf ("Please enter ten intergers now.  Seperate each number by a space\n");
    	for ( i = 0; i < 10; i++) {
    		scanf ("&#37;d", &array1[i]);
    	}
    
    	printf ("Please enter ten intergers now.  Seperate each number by a space\n");
    	for ( i = 0; i < 10; i++) {
    		scanf ("%d", &array2[i]);
    	}
    alternatively you can also write

    Code:
        for ( i = 0; i < 10; i++) {
    		scanf ("%d", (array1+i));
    	}
    make a concept, scanf() function always put the given value at the given address in the given format.
    here array1 --- gives the base address of array1. hence scanf() will put the value at this location which finally will be value of first element. and (array1+i) means putting the value at each next location till the array size;

    Code:
    	/*int add1and2 (array3[9]);*/
    as according to add1and2() definition, it supposed to pass three arguments like this

    add1and2(arr1 , arr2 , arr3);

    with this, compiler understand that the base addresses(address of first elements) of arrays are being passed which may further be iterated through its each element.


    Code:
    int add1and2 (array1[], array2[], array3[])
    you should rather write
    Code:
    int add1and2 (int array1[], int array2[], int array3[])
    the formal arguments need to be defined fully;



    Code:
    	int *point2a1 = &array1[];
    	int *point2a2 = &array2[];
    	int *point2a3 = &array3[];
    applying subscript operator only means you are assigning the address of unknown entity.
    you should rather write
    Code:
    int *point2a1 = &array1[0];
    or
    Code:
    int *point2a1 =array1;

    Code:
    	 for(i = 0; i < 10; i++,) {    
    		 array3[i] = *point2a1[i] + *point2a2[i]; 
    	 }
    you code is write though, however it doesn't make any sense if you dont understand what you have written;

    Code:
    array3[i] = *point2a1[i] + *point2a2[i];
    can be understood as

    Code:
    *(array3+i) = *(*(point2a1+i)) + *(*(point2a1+i)) ;
    means, you are pointing to the same value two times using * operator. although this is not a error prone but it is senseless too.
    Last edited by san_crazy; 10-02-2008 at 11:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  2. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  3. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM
  4. Arrays of Pointers
    By phatslug in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2002, 12:04 AM
  5. Pointers and Arrays
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 06-16-2002, 01:18 PM