Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Use of pointers

  1. Jan 16, 2010 #1

    AK2

    User Avatar

    Code (Text):
    #include <stdio.h>
    #define K 10
    int array1[K], array2[K];
    int *ptr;
    int addarrays(int x[],int y[]);
    main()
         {
          int count;
          for (count = 0;count < K; count++)
             {
               puts("Enter numbers into array1");
               scanf("%d", array1[count]);
             }
           for (count = 0;count < K; count++)
             {
               puts("Enter numbers into array2");
               scanf("%d", array2[count]);
             }
           [B]ptr = addarrays(array1,array2);[/B]
           for (count = 0;count < K; count++)
              {
               printf("%d + %d = %d\n",array1[count], array2[count], *(ptr+count));
              }
           return 0;
    int addarrays(int x[], int y[])
          {
           int count, total[K];
           for(count = 0; count< K; count++)
              {
               total[count] = array1[countk] = array2[count];
              }
           return total;
          }
    i compiled this code in dev c++ ide and i got an error on the bolded line. The purpose of the code is two add two arrays and put the results in a new array and print the three arrays. The function addarrays is meant to return a pointer to the first array element to the calling program. This is where the error lies.
     
  2. jcsd
  3. Jan 16, 2010 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Three problems here:
    1. You declared addarrays as a function that returns an int but you are assigning the result to a variable declared as an int*. This is the cause of the immediate problem.
    2. addarrays returns an int*, contrary to the declared return value (an int). You probably got a compile error on the return statement from addarrays as well.
    3. You are returning a pointer to a function-scope automatic variable in addarrays.

    You can fix the first two problems by declaring and defining addarrays as a function that returns an int*. This will not fix the third problem. That return statement in addarrays is completely invalid -- and most compilers will not catch it. You are invoking undefined behavior here! Never return a pointer to a function-scope automatic variable. One way to fix this problem is to make total a static variable. This will create other problems (it makes your function non-reentrant), but these are probably beyond the scope of the class you are taking.
     
  4. Jan 16, 2010 #3

    AK2

    User Avatar

    Thanks for the help. im still a beginner. C is the first computer language i am learning. I am using Sams Teach Yourself C. I am currently on the 9th chapter(Understanding Pointers) of the 21 chapter book. I have not yet covered the terms static variable and function-scope automatic variable. I will probably come back to the question when I have reached those terms in the book. The question I posted is a do it on your own question in the book im using to learn C. I did what you asked me to do in the first two points and as you said earlier it didnt solve the last problem.
     
    Last edited: Jan 16, 2010
  5. Jan 17, 2010 #4

    Mark44

    Staff: Mentor

    Aside from the problems that DH points out, your addarrays function is either misnamed or has a semantic error in it. A casual peruser of your code would expect addarrays to add two arrays together, and return an array (actually an array address, which is essentially the same as an int * value). That's not what it does -- you probably meant for the 2nd assignment in the bolded line below to be +.

    Note that fixing this alone doesn't solve your problems.

    Code (Text):
    int addarrays(int x[], int y[])
    {
       int count, total[K];
       for(count = 0; count< K; count++)
       {
           [B]total[count] = array1[countk] = array2[count];[/B]
       }
       return total;
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook