Question about arrays and pointers

+1 Shlesh Tiwari · January 15, 2016
Hi all, 

The following code keeps on crashing. Can someone tell me what am I doing wrong here?

int main()
    int *num;
    printf("Enter a number\n");
    scanf(" %d", &num);
    printf("The number you entered is %p", *num);
    return 0;

Another question I had was - if we declare num as a pointer variable, then while storing the variable using scanf(), we shouldn't be using the "&" anymore, right? In Bucky's code (Tutorial 48), he creates a pointsarray pointer variable, but to store the elements in it using the for loop, he uses "&pointsarray". 

On the other hand, in the code above, if I use "&num", it gives me a warning, while if I omit the "&", it does not give me a warning for that instruction. 
The program still crashes in either case. 

Also, if using the pointer variable for the array essentially points to the location where the string is stored, and does not actually store the string in itself (as a copy like in the case of a regular array), then how can we make changes to the pointer variable without dereferencing it?


char array1[20] = "Hello there";
char *parray = "Who's there";

what is the difference between the following :

array, &array, array, &array,  parray, &parray, *parray, parray, &parray, *parray ?

Same question for integers, because somehow they seem to behave differently.
Kindly help me out here. 

Thanks in advance. 

Post a Reply


Oldest  Newest  Rating
0 Shlesh Tiwari · January 15, 2016
Okay I am thoroughly confused right now. 

printf("The number you entered is %p", *num); You are trying to access *num.

I am, because I want to print the value of num. And since num is a pointer, I want it to return the value present in the address that it contains, right? Is that not it? 

Yes. The variable is a house while the pointer is the house number.

The following program runs perfectly:
int main()
    int *num;
    printf("Enter a number\n");
    scanf(" %d", &num);
    printf("The number you entered is %d", num);
    return 0;
 I did end up using the "&" now though. 

No, he uses&pointsarray[]. The dereferencespointsarray the same way *pointsarray does(plus an offset).

How are "&" and "*" performing the same function? Aren't they supposed to do opposite things?

You can't. The C standard forbids the use of example two. You must use :
char const * parray...
char const * constparray... (I can't remember if you are allowed to assign something to parray pointer after initialization)

Bucky did not use any of those commands but still modified the contents of pointsarray. How is that? 

And I apologise for this extreme confusion, but I am new to programming and it is difficult to wrap my head around all this. Could you also explain your other answers in detail? I didn't quite understand them. I would be really grateful. 

Thanks so much.
0 Nahasat Hossain · January 18, 2016
in C programming #48 which is creating an expandable program using Heap .
well my question is, in the code we are trying to get the average.. and i've attached a photo right with this comment/images/forum/upload/2016-01-18/ may see that the average of 21,28,87,62,4 is 56.999 but if i calculate it in the calculator the average shows 40.4
can anyone tell me what the hack is going on?? :(
0 Linguist Llama · January 19, 2016
float average = 0.0; // What value does average have?
int total; // What value does total have?

Do you see your mistake, yet?

One more thing, if I were still a moderator I would have deleted your post because you just effectively hijacked someone elses thread... Please start a new topic when you want to ask a question.
0 Linguist Llama · January 19, 2016
Also, it might be merely coincidental that you cut off the warnings for your screenshot, but YOUR COMPILER IS WARNING YOU ABOUT YOUR PROBLEM!
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator