Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

> need help with c programming

post Nov 28 2021, 01:28 AM

New Member
Junior Member
11 posts

Joined: Jul 2015
in C, while types like int, double hold values, arrays are pointers (addresses in memory)

int n = 5 (variable n holds integer 5)
printf(n) // prints value (5)
printf(&n) // prints 0x8f49be30 (some address in memory)

char x[10] = some letters
printf(x[n]) // prints letter (value) at specified index
printf(&x[n]) // prints pointer to specified index
printf(x) // prints pointer to the first index

As you know, arrays are indexed therefore you only get the value when you call its index. However, when calling x itself, it will return you the pointer of the address of the first index. So theres no such thing as &x (address of pointer)
x[n] returns value
x returns pointer
address : &x[0] is the same as x
address : &x[1] is the same as x+1
value : x[0] is the same as *x (dereference operator to get value pointed by pointer)
value : x[1] is the same as *(x+1)
I believe you understand how scanf works based on other comments here

This post has been edited by Horhorlidat: Nov 28 2021, 01:47 AM


Change to:
| Lo-Fi Version
0.0165sec    0.64    6 queries    GZIP Disabled
Time is now: 24th January 2022 - 07:54 AM