Adding Arrays Together with Pointers in C

  • Thread starter Thread starter AK2
  • Start date Start date
  • Tags Tags
    Arrays Pointers
AI Thread Summary
The discussion focuses on a C programming code that aims to add two arrays and print the results. Key issues identified include incorrect function return types and undefined behavior due to returning a pointer to a local variable. The function `addarrays` is declared to return an `int`, but it is intended to return an `int*`, leading to a type mismatch. Additionally, the code incorrectly assigns values in the loop, using an assignment operator instead of an addition operator, which does not achieve the intended functionality of adding the arrays. Suggestions for resolution include changing the return type of `addarrays` to `int*` and making the `total` array static to avoid returning a pointer to a local variable. However, this static approach has its own drawbacks, such as making the function non-reentrant. The original poster is a beginner in C programming and is currently learning about pointers, indicating a willingness to revisit the topic once they cover more advanced concepts in their studies.
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.
 
Technology news 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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
23
Views
2K
Replies
2
Views
1K
Replies
19
Views
5K
Replies
22
Views
3K
Replies
7
Views
2K
Replies
2
Views
11K
Back
Top