0

I have the following simple code in C:

void main()
{
    int* s;
    int a = 5, b = 3;
    sum(a, b, s);
    printf("%d + %d = %d", a, b, *s);
}
void sum(int a, int b, int* s) {
    *s = a + b;
}

the program compiles, but gives a runtime error. Why?

Process returned -1073741819 (0xC0000005) execution time : 0.972 s Press any key to continue.

serge
  • 13,940
  • 35
  • 121
  • 205

3 Answers3

2

First of all, avoid implicit function declaration

#include <stdio.h>

void sum(int a, int b, int* s);

void main()
{
    int s;
    int a = 5, b = 3;
    sum(a, b, &s);
    printf("%d + %d = %d", a, b, s);
}

void sum(int a, int b, int* s) {
    *s = a + b;
    return;
}

This prints 8. If you still wish to use int* s then allocate space for a single int.

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

void sum(int a, int b, int* s);

void main()
{
    int* s = malloc(sizeof(int)*1);
    int a = 5, b = 3;
    sum(a, b, s);
    printf("%d + %d = %d", a, b, *s);
}

This will also print 8.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • how you pass to the fucntion an int, s, when you have to pass an "int *"? – serge Sep 27 '20 at 22:40
  • @Serge I didn't pass an int. I passed `&s` which is the address of an int. ---- `int* s` is a pointer to int. It stores the address of int (that's how it points...), by passing `&s` I am passing the address of `s` which is an address of an int. – Tony Tannous Sep 27 '20 at 22:41
  • ok, so, the compiler is not smart enough to do the malloc for me? – serge Sep 27 '20 at 22:45
  • 2
    @Serge I'm offended on behalf of the compiler. Remember, the compiler has no way to tell how much memory you may want to allocate. In your case it's a single `int` but it could have been a block of 1000 ints... – Tony Tannous Sep 27 '20 at 22:47
  • thanks now I understand ) I used your second version, that is good for me – serge Sep 27 '20 at 22:49
  • 1
    @Serge don't forget to include stdlib. I forgot to paste it. Let me edit. – Tony Tannous Sep 27 '20 at 22:49
  • however, I don't really like the compiler... It could automatically realocate memory if I would assign 1000 ints instead of one.... – serge Sep 27 '20 at 22:55
  • @Serge maybe you should try [tag:c++] then. It has self-adapting containers doing the allocation on your behalf. – Tony Tannous Sep 27 '20 at 22:59
1

s is an int* which means that it can store the address of an int. However, you never made it do so. Therefore, when sum dereferences it, you're dereferencing an invalid address (i.e., whatever junk stack data s was assigned when main began).

You need to do something like

int main() {
    int a, b, c;

    a=5;
    b=3;

    sum(a,b,&c);
    printf("%d + %d = %d", a, b, c);

    return 0;
}

Or, if you want to use an int* variable,

int main() {
    int *s;
    int a, b, c;

    a=5;
    b=3;
    s=&c;

    sum(a,b,s);
    printf("%d + %d = %d", a, b, *s);

    return 0;
}
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • I don't really understand... suppose int* s is pointing to a random location in the memory. Why does this random location value does not change to a+b in the "sum" function? – serge Sep 27 '20 at 22:37
  • Because that's not what `*s=a+b;` does. That command instead stores the value of `a+b` into the integer whose address is stored in `s`. There is no such integer. – Daniel Walker Sep 27 '20 at 22:39
  • 1
    @Serge You may dereference a pointer that points to a valid object. – Vlad from Moscow Sep 27 '20 at 22:39
  • If you want to change the value of `s`, you need to do something like `s = ...`. – Daniel Walker Sep 27 '20 at 22:40
1
int* s;

Here you're creating a pointer to int. But if you haven't located any memory for the int yet.

A possible solution would be to declare the same way you did with a and b, and then pass its reference to the function.

void main()
{
    int s;
    int a = 5, b = 3;
    sum(a, b, &s);
    printf("%d + %d = %d", a, b, s);
}
void sum(int a, int b, int* s) {
    *s = a + b;
}
89f3a1c
  • 1,430
  • 1
  • 14
  • 24