Adding Arrays Together with Pointers in C

  • Thread starter Thread starter AK2
  • Start date Start date
  • Tags Tags
    Arrays Pointers
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
AK2
Messages
39
Reaction score
0
Code:
#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.
 
on Phys.org
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.
    [*]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.
    [*]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.
 
D H said:
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.
    [*]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.
    [*]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.


Thanks for the help. I am 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 I am 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:
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:
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;
}