Adding Arrays Together with Pointers in C

In summary, the code has an error in it that causes it to do something unintended. You will also need to fix the declaration of addarrays as a function that returns an int* to make it work correctly.
  • #1
AK2
39
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
  • #2
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.
 
  • #3
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.
  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.

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:
  • #4
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;
}
 

1. How do I add two arrays together using pointers in C?

To add two arrays together using pointers in C, you will need to declare two arrays and a pointer variable. Then, use a for loop to iterate through both arrays and add the values at each index together using pointer arithmetic. Finally, store the result in a new array or print it out directly.

2. What is the benefit of using pointers to add arrays in C?

The use of pointers in adding arrays in C allows for more efficient memory usage and manipulation. Pointers allow you to directly access and modify the values in an array without creating additional copies of the data. This can save time and resources, especially when dealing with large arrays.

3. Can I add arrays of different sizes using pointers in C?

Yes, you can add arrays of different sizes using pointers in C. However, you will need to make sure to handle any potential errors or unexpected behavior that may occur due to the difference in sizes. This can be done by using conditional statements or by verifying the sizes of the arrays before performing the addition.

4. Is it possible to add multiple arrays together using pointers in C?

Yes, it is possible to add multiple arrays together using pointers in C. You can achieve this by creating a new array to store the result, then using a for loop to iterate through each array and add the values using pointer arithmetic. This process can be repeated for as many arrays as needed.

5. Can I use pointers to add arrays of different data types in C?

Yes, you can use pointers to add arrays of different data types in C. However, you will need to make sure that the data types are compatible and can be added together. For example, you cannot add an array of integers to an array of strings using pointers, as they are different data types.

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top