1

I am trying to create a program in C that calculates the sum of two int arrays using pointers. Here is an example of what I want to do:

int a[] = {1,2,3,4}
int b[] = {1,2,3,4,5,6}
int c[] = sumArrays(a,b,4,6)
Output : c = {2,4,6,8,5,6}

The problem is my output is different, it shows:

Output : c = {2,4,6,8}

Any idea what did I do wrong and how to correct it ? Here is my code :

#include <stdio.h>
#include <stdlib.h>

int* sumArrays(int *arr1, int *arr2, int dim1,int dim2)
{
    int *ret = NULL;

    if(dim1<dim2) ret = (int*) malloc(dim2*sizeof(int));
    else ret =  (int*) malloc(dim1*sizeof(int));
    if(ret)
    {
        if(dim1<dim2) {
            int i = 0;
            for (i = 0;i < dim1;i++) {
                ret[i] = arr1[i] + arr2[i];
            }
            for (i = dim1; i < dim2;i++) {
                ret[i]=arr2[i];
            }   
        } else {
            int i = 0;
            for (i = 0;i < dim2;i++) {
                ret[i] = arr1[i] + arr2[i];
            }
            for (i = dim2; i < dim1;i++) {
                ret[i]=arr1[i];
            } 
        }
    }
    return ret;
}


int main()
{
int *a[] = {1,2,3,4};
int *b[] = {1,2,3,4,5,6};

int *c = sumArrays(a,b,4,6);
printf("c = ");
int i;
for (i = 0; i < sizeof(c); i++) {
    printf("%d,",c[i]);
} 
}
4
  • Don't flag C++, else, you will have to use std::vector. Commented May 14, 2017 at 16:24
  • You also need to change int *a[] to int a[]. Commented May 14, 2017 at 16:28
  • On another note, you should be getting c = {2,4,6,8,}, not c = {2,4,6,8} Commented May 14, 2017 at 16:45
  • Btw, the code is incorrect. It could lead to undefined behaviour. You are allocating some memory inside a function and when you go out of its scope, who knows. Never do that Commented May 14, 2017 at 17:04

2 Answers 2

5

Sizeof c will always return 4 for 32-bit system and 8 for 64-bit, because c is a pointer to int.

So to print the result array you should write:

for (i = 0; i < 6; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

That is so obvious.. Thank you !
1

You have problem about using pointers.

int *a[] = {1,2,3,4};      // a is an array of pointers to integers.
int *b[] = {1,2,3,4,5,6};  // b is an array of pointers to integers.

By doing this, you are declaring array of pointers to integers. So, it most likely causes compiler warning or error depending on your compile settings like, initialization makes pointer from integer without a cast. When you are passing the actual arguments to formal parameters, it causes same warning as well. Your main should be like this,

int main()
{
    int a[] = {1,2,3,4}; 
    int b[] = {1,2,3,4,5,6};
    int sizeA = sizeof(a) / sizeof(*a);
    int sizeB = sizeof(b) / sizeof(*b);


    int *c = sumArrays(a,b,sizeA,sizeB);
    printf("c = ");
    int i;
    for (i = 0; i < (sizeA < sizeB ? sizeB : sizeA); i++) {
        printf("%d,",c[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.