arrays

#include<stdio.h>
int main()
{
char s[]="get organised!learn C!!";
printf("%s\n",&s[2]);
printf("%s\n",s);
printf("%s\n",&s);

return 0;
}
output:
t organised!learn C!!
get organised!learn C!!
get organised!learn C!!
can anyone explain the ouput especially for the first printf.In the sense how it works??

Comments

  • Well from my limited knowledge char s with the square brackets is an stringor array of characters, in this case "get organised!learn C!!".

    In the first printf note the [2], which means the printf starts at the 3nd character,(taking into account the first character is assigned the value of [0], the the second chacter[1] and the third character [2]).

    %s is the string placeholder, meaning the value of the variable after the comma has to be a sting(or array of characters, same thing) or array of strings or single character. If you wanted to printf a number you would use a %d placeholder. The value of the char s variable( in this instance a string saying (get organisied learn c) is given/swapped with the $s string placeholder and printed out with printf. If you wrote printf"(%d\n",s) you would get an error as a string can't be converted to a number.(I think, might be converted to some ascii character).

    Lastly, I think the ampersand is only need for certain placeholders and not strings but again I can't even be called an amateur let alone an expert.

    The /n is a newline character putting a newline between the printf statements.
  • If you wrote printf"(%d\n",s) you would get an error as a string can't be converted to a number.

    There will be no error. printf() will assume the argument is an integer instead of a string and will attempt to convert the first 4 bytes (or whatever the size of an integer is) of the string to integer. If the bytes do not contain numeric digits then printf() will just display 0 as the result.
  • printf("%s\n",s);
    What is 's', after the comma? That's an address to the first element in the char array. So the entire string is printed.

    printf("%s\n",&s[2]);
    What is &s[2]? That is the address of s plus two spaces in the array, where each space is the size of the data type. Here, char=1, so it's just s+2 bytes.

    printf("%s\n",&s);
    &s is the address of the address of 's'. In this case, it's like using a pointer, to point to another pointer, which points to the base of the 's' array.
  • thanks a lot!!
  • Adak wrote:
    printf("%s\n",s);
    ...
    printf("%s\n",&s[2]);
    What is &s[2]? That is the address of s plus two spaces in the array, where each space is the size of the data type. Here, char=1, so it's just s+2 bytes.
    ...

    Maybe is more correct to say that s[2] is the third char in the string (pointed as "s+2" as you say) and the & takes the address of that char. At the end, it is the pointer "s+2" but is found in an alternative way.

    Let me say:

    s[2] -> type char
    &s[2] -> type char *
    s+2 -> type char *

    In this case, using "s+2" appears to be more clear and professional than using &s[2] but the clearness depends from the reader.
  • >In this case, using "s+2" appears to be more clear and professional than using &s[2]

    I've seen it both ways, but &s[2] is more frequent in professional programs.
Sign In or Register to comment.