Adding Arrays Together with Pointers in C

  • Thread starter Thread starter AK2
  • Start date Start date
  • Tags Tags
    Arrays Pointers
Click For Summary

Discussion Overview

The discussion revolves around a C programming code snippet that attempts to add two arrays together using pointers. Participants analyze the code for errors related to function return types, variable scope, and logical errors in the implementation of the addition function. The scope includes technical explanations and debugging of code for beginners in programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies that the function addarrays is declared to return an int but is assigned to an int*, which is a source of compilation errors.
  • Another participant points out that returning a pointer to a function-scope automatic variable leads to undefined behavior, suggesting that total should be declared as a static variable to avoid this issue, although this introduces other complications.
  • There is a suggestion that the logic within the addarrays function is incorrect, as it appears to assign values incorrectly rather than adding them, which could lead to confusion about the function's purpose.
  • The original poster expresses their beginner status in C programming and mentions that they have not yet covered certain concepts, indicating a willingness to revisit the question later as they progress in their studies.

Areas of Agreement / Disagreement

Participants generally agree on the identification of multiple issues within the code, but there is no consensus on the best approach to resolve the problems, particularly regarding the handling of variable scope and function return types.

Contextual Notes

Limitations include the original poster's lack of familiarity with certain programming concepts such as static variables and function-scope automatic variables, which may affect their understanding of the proposed solutions.

Who May Find This Useful

Beginners in C programming, particularly those learning about pointers and array manipulation, may find this discussion helpful as it addresses common pitfalls and debugging strategies.

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

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
20
Views
2K
Replies
3
Views
2K
Replies
12
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
6K
Replies
12
Views
2K