+ 1

What is this?(see code).

I did notice how this 6 addition is removing 6 characters from printf.but what we call this thing.seeing for first in c challenge. https://code.sololearn.com/ctDbL25okaZL/?ref=app

5th Dec 2021, 10:33 AM
gaurav kumar
gaurav kumar - avatar
3 Answers
+ 6
It's pointer arithmetic. The expression *(a + n), where a is an array and n is an integer, is equivalent to a[n]. If you add an integer to a pointer you'll get to the location of n-th element of a. C doesn't really have the concept of strings rather it's considered as a char pointer. The program will look like this, char *ptr="hello world"; printf( 6 + ptr); which is &ptr[6]  this address points to "world" https://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Pointers.html#:~:text=Pointer%20arithmetic%20and%20arrays,-Because%20pointers%20are&text=Add%20an%20integer%20to%20a,aren't%20the%20same).
5th Dec 2021, 11:03 AM
Minho
Minho - avatar
+ 4
// Watch what happens. :) #include <stdio.h> #include <string.h> int main() { char s[] = "gaurav kumar"; for (int x=0; x<strlen(s); x++) printf("%c ", s[x]); return 0; }
5th Dec 2021, 11:20 AM
SoloProg
SoloProg - avatar
+ 2
gaurav kumar I don't understand it myself, but it references the index of the string. Change the number and you will see different outputs. Indexing starts from 0, spaces included
5th Dec 2021, 10:41 AM
Rik Wittkopp
Rik Wittkopp - avatar