0

I am having a crazy output with funny characters (Φw    ÅΩw) can i know what's wrong in the code? probably the int main is wrong i am obliged with int sumArray (int * a, int len , int * sum )format

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

int sumArray(int *a, int len, int *sum) {
  int sum1 = 0;
  if (a == NULL || sum == NULL)
    return -1;
  int i;
  (*sum) = 0;
  for (i = 0; i < len; i++) {
    (*sum) += a[i];
  }

  return 0;
}

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

  sumArray(&a, 4, &b);
  printf(b);
  return 0;
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29

2 Answers2

2

Can you try these changes ?

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

int sumArray(int *a, int len, int *sum) {
  // int sum1 = 0;  // i removed this variable because you are not using it
  if (a == NULL || sum == NULL)
    return -1;
  int i;
  (*sum) = 0;
  for (i = 0; i < len; i++) {
    (*sum) += a[i];
  }

  return 0;
}

int main() {
  // int *a = {1, 2, 3, 4};
  int a[] = {1, 2, 3, 4};
  int b;
  // i rather declare an integer instead of a pointer to an integer
  // when you declared int * b , this was a pointer, and your printf(b) was
  // printing an address, not the value calculated by sumArray that is why you
  // were printing funny characters

  sumArray(a, 4, &b);
  // a is already a pointer

  printf("%d", b);
  return 0;
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29
Hayfa
  • 147
  • 1
  • 13
0

You are using your pointers uninitialized. When you create a pointer, you don't know where the pointer points to. It either will be pointing to some garbage data, or in worse case, it will be pointing to a memory region which is already being used by some other program in your computer or maybe by OS itself.

If you really want to use pointers like this, you should dynamically allocate memory for them.

int* a = malloc( 4 * sizeof(int) );
int* b = malloc( sizeof(int) );

This makes sure that you can assign four integers to the memory region to which a points to. And one for b.

You then can wander in that memory using loops to assign, read or write data.

for ( int i=0; i < 4; i++ )
{
    *(a + i) = i + 1;
}

Here we have a for loop which will run 4 times. Each time we are moving one block in the memory and putting the number we want there.

Remember, a is a pointer, it points to the beginning of a 4 int sized memory region. So in order to get to the next block, we are offsetting our scope with i. Each time the loop runs, a + i points to the "ith element of an array". We are dereferencing that region and assigning the value we want there.

for ( int i=0; i < 4; i++ )
{
    printf("%d\n", *(a + i) );
}

And here we are using the same logic but to read data we just write.

Remember, you need to use format specifiers with printf function in order to make it work properly. printf() just reads the whatever data you happened to give it, and format specifier helps interpret that data in given format.

If you have a variable like int c = 65; when you use %d format specifier in the printf you will read the number 65. If you have %c specifier in the printf, you will read letter A, whose ASCII code happens to be 65. The data is the same, but you interpret it differently with format specifiers.

Now, your function int sumArray(int *a, int len, int *sum) accepts int pointer for the first argument. In the main function you do have an int pointer named a. But you are passing the address of a, which results in double indirection, you are passing the address of a pointer which holds address of an int array. This is not what you want, so & operator in the function call is excess. Same with b.

Call to the sumArray should look like

sumArray( a, 4, b );

And lastly, we should fix printf as well. Remember what I said about format specifiers.

And remember that b is not an int, it's int*, so if you want to get the value which b points to, you need to dereference it.

In the end, call to printf should look like

printf( "%d", *b );

Also, you should remember to free the memory that you dynamically allocated with malloc. When you use regular arrays or variables, your compiler deals with these stuff itself. But if you dynamically allocate memory, you must deallocate that memory using free whenever you are done with those pointers.

You can free a after the call to sumArray and b before terminating the main function like

free(a); and free(b);

In these kind of small projects freeing memory is probably won't cause any unwanted results, but this is a very very important subject about pointers and should be implemented properly in order to settle the better understanding of pointers and better programming practice.

In that form, your code should work as you intended.

BUT... And this is a big but

As you can see, to make such a simple task, we spent way more effort than optimal. Unless your goal is learning pointers, there is no reason to use pointers and dynamic allocation here. You could have used regular arrays as @Hayfa demonstrated above, and free yourself from a lot of trouble.

Using pointers and dynamic memory is a powerful tool, but it comes with dangers. You are playing with actual physical memory of your computer. Compilers nowadays won't let you to screw your OS while you are trying to add two numbers together but it still can result in hard to detect crashes especially in complex programs.

(Sorry if it's hard to read, I am not necessarily confident with text editor of Stack Overflow.)

aulven
  • 521
  • 3
  • 14