QUOTE(Grammar Police @ Nov 17 2021, 11:34 PM)
this doesnt work, unless you change
scanf("%i", &n);
n = valuescanf("%i", &n);
&n = address
Think of it as "scan feed into value n" vs "scan feed into address n". Which do you think is correct?
QUOTE
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?
You will typically use pointers in function parameters to avoid passing the size of the value into the function, using unnecessary memory.
QUOTE
Also if you want to use string in c, you have to do like char *string, so all strings are secretly arrays?
Yes, its the same in almost any language. Strings are really just an array of chars, or maybe even a linked list.
QUOTE
Also why is
char *s = "xyz";
printf("%s", s);
this is OK, but
printf("%s", s[1]);
This is NOT ok
and instead need to put the & like this to work
printf("%s", &s[1]);
??
%s is a string, which as you've learnt, is an array of characters, so its expecting a pointer/address. s[1] gives the value, not the address. If you changed %s to %c in printf, s[1] will work.char *s = "xyz";
printf("%s", s);
this is OK, but
printf("%s", s[1]);
This is NOT ok
and instead need to put the & like this to work
printf("%s", &s[1]);
??