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.

4
  • pArr or ptr? Also, in your loop k will be sizein the last pass, which isn't a valid index for an array of size size. Commented Mar 16, 2016 at 12:50
  • Where is the definition of pArr? Commented Mar 16, 2016 at 12:52
  • for (k=0; k<sizeArr; k++) { printf("please enter element number %d\n", k); scanf("%d", ptr+k); } Commented Mar 16, 2016 at 12:52
  • Edited it. It should've been *(ptr+k)..but as you guys explained: just ptr+k. Commented Mar 16, 2016 at 13:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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.

1 Comment

Thanks for the explanation.

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.