Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

> need help with c programming

TSGrammar Police
post Nov 17 2021, 11:34 PM, updated 7 months ago

Junior Member
332 posts

Joined: Mar 2016
this c programming has been giving me lot of grief...

why is

char x[10];

scanf("%s", x);
printf("%s", x);

this works?


int n ;
scanf("%i", n);
printf("%i", n);

this doesnt work, unless you change

scanf("%i", &n);

Is it because char is an array? But shouldnt pointers only be used when there is an array? actually when should you use a pointer?

Also if you want to use string in c, you have to do like char *string, so all strings are secretly arrays?

Also why is

char *s = "xyz";

printf("%s", s);

this is OK


printf("%s", s[1]);

This is NOT ok

and instead need to put the & like this to work

printf("%s", &s[1]);

TSGrammar Police
post Nov 23 2021, 11:54 AM

Junior Member
332 posts

Joined: Mar 2016
QUOTE(silverhawk @ Nov 23 2021, 10:26 AM)
n = value
&n = address

Think of it as "scan feed into value n" vs "scan feed into address n". Which do you think is correct?

cannot scan feed into integer and must feed into address? why is it?


Change to:
| Lo-Fi Version
0.0176sec    0.35    6 queries    GZIP Disabled
Time is now: 29th May 2022 - 11:07 AM