1

So I have a pointer and a size of the array that gets passed down to a function that asks for user input and stores user input as array elements.

So the function looks like this:

int addElements(int * ptr, int sizeArr) {

int k=0;
for (k=0; k<=size; k++) {
    printf("please enter element number %d\n", k);
    scanf("%d", *(ptr+k));
}

return pArr;

}

It returns a pointer because I need to work further with the array.

I get

Segmentation fault (Core dumped)

error when inputting the elements to the array.

What am I doing wrong here? I can update the array elements (from random/automatic values ) to 0 just fine it's just the scanf that can't write to the array.

Bruce Strafford
  • 173
  • 4
  • 15

3 Answers3

1

ptr is a pointer to int.

(ptr + k) is still a pointer to (a different) int. *(ptr + k) is an int.

scanf( "%d", <some_int> ) will interpret the number stored in that int as an address, and try to write whatever the user entered to that "address".

Which is undefined behaviour.

You probably want to omit the * turning the pointer into an int; the statement would be well-formed then.

Generally speaking, recommended reading: How to read / parse input in C.

  • Check the return value of scanf(), as the user might have entered something that is not a number.
  • Cater for not-a-number input still waiting in the input buffer by reading up to the next newline, or subsequent scanf( "%d", ... ) will fail as well.
  • Better yet, read lines of input (with fgets()), and parse them in-memory (with strtol()), which allows you to recover from faulty input more gracefully.
Community
  • 1
  • 1
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Hi. Thanks for the explanation and for the link. I'm just learning C and pointers and arrays are taking a while to ge tused to. – Bruce Strafford Mar 16 '16 at 13:08
1

Modify the way you read elements by changing

scanf("%d", *(ptr+k));

with

scanf("%d", (ptr+k));

Scanf needs a pointer next to the specifier, but ptr is already a pointer (and so is ptr+k ) so by using the asterisk you imply the contents of the pointer and not the pointer itself.

Marievi
  • 4,951
  • 1
  • 16
  • 33
1

For any pointer (or array) pArr and index k the expression *(pArr + k) is equivalent to pArr[i].

The problem with that is that pArr[i] (and therefore *(pArr + k)) is not a pointer, which is required by scanf to work.

There are two solutions: The first is to continue using pointer arithmetic and then simply drop the dereference operator (so you pass only pArr + k to scanf); Or you use "normal" array indexing and add the address-of operator (as in &pArr[k]).


A little explanation about the alternatives. The expression pArr[k] gives you the value at index k in the array, to get a pointer to that value we add the address-of operator & and get &pArr[k].

And since pArr[k] is equivalent to *(pArr + k) then &pArr[k] would be equivalent to &*(pArr + k), but the address-of and dereference operator cancels each other out leaving you with pArr + k.


What is happening now with the code as shown in the question is that you get the value at index k (i.e. pArr[k]) and the compiler converts it to a pointer (it should give you a warning about it though) and then scanf write to the memory pointed to by that value. Since pArr[k] is not a valid pointer, it may even be uninitialized (and therefore have an indeterminate value) you will have undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621