A weird declaration: int *a="any_string"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shadyabhi
    New Member
    • Dec 2008
    • 18

    A weird declaration: int *a="any_string"

    Code:
    #include<stdio.h>
    int main(void)
    {
    	int *p="Hello_i_am_abhijeet";	//Whats this actually doing???
    	char q[]="Hello_i_am_abhijeet";	//This is OK
    	
    	printf("%c %c\n",p[2],q[2]);	      //Here p[2] shows "a" that means 
    						      //due to p[2], pointer moves 8 bytes 
    	return 0;
    }
    Terminal Output:

    codefire@codefi re-desktop:~$ gcc test.c
    test.c: In function ‘main’:
    test.c:4: warning: initialization from incompatible pointer type
    codefire@codefi re-desktop:~$ ./a.out
    a l
    codefire@codefi re-desktop:~$



    I want to know as to what actually happens in line 4 of program.
    I know line 4 may look weird.
    But, i was curious to know. And hows is the output coming, i mean whats theory behind that.

    Also, it would be great if you pls help me understanding the difference between "char *a" and "char a[]". I tried searching on net but couldnt get satisfied with what i understood.
    Your reply will be of great help to me.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Yes, it's mighty weird. Like it doesn't compile. "Hello_i_am_abh ijeet" is a literal string which is a char array. The address of the H is a char*. You cannot assign a char* to an int*.

    If your compiler accepts this, then get a proper compiler.

    Next:
    Code:
    char* c = "Hello_i_am_abhijeet";
    char  d[] = "Hello_i_am_abhijeet";
    In this example c is a pointer to a char. That char is the H of Hello. The literal itself is a char array but is constant so you cannot chnage the letters in the array.

    On the other hand, d is an array of char but the number of elements is not provided. In this case the compiler counts the letters in "Hello_i_am_abh ijeet" and adds 1 for a null terminator. Then it creates the d array for that number oc characters and copies "Hello_i_am_abh ijeet" to the d array. This time d is not constant so you can change the letters in the array. This only works when d is created. Later on if you try:

    Code:
    d  = "New letters";  //ERROR
    d[] = "New letters";  //ERROR
    you get errors. To change d you would copy in new values:
    Code:
    strcpy(d, "New letters");
    strcpy does not check that the new letters will fit in the d array. It's up to you to manage this.

    Comment

    • shadyabhi
      New Member
      • Dec 2008
      • 18

      #3
      @weaknessforcat s

      I used GCC as it can be understood through the terminal output i posted above..

      But, still i didnt get the answer of what I actually asked.
      Can anybody answer? I am really curious about this peculiarity.

      Comment

      • shadyabhi
        New Member
        • Dec 2008
        • 18

        #4
        @weaknessforcat s

        I used GCC as it can be understood through the terminal output i posted above..

        But, still i didnt get the answer of what I actually asked.
        Can anybody answer? I am really curious about this peculiarity.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by shadyabhi
          I want to know as to what actually happens in line 4 of program.
          Nothing happens in line 4 -- it doesn't compile.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by shadyabhi
            Also, it would be great if you pls help me understanding the difference between "char *a" and "char a[]". I tried searching on net but couldnt get satisfied with what i understood.
            Your reply will be of great help to me.
            Code:
            char *a = "This is a string";
            char b[] = "So is this";
            In the first case, the string (characters + null termination) is placed in an unnamed location and the address of that location is stored in variable a. sizeof(a) = the size of a pointer (typically 4 bytes).

            In the second case, the string (characters + null termination) is placed in a buffer starting at location b. sizeof(b) = 11.

            Comment

            • shadyabhi
              New Member
              • Dec 2008
              • 18

              #7
              thanx for reply ...
              But, i tell you dude, that program(the exact code which i posted) compiles gcc 4.2.4...
              Which compiler are you using??

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                >But, i was curious to know. And hows is the output coming, i mean whats theory behind that.

                Assuming that program executes on little-endian machine, printf("%c", p[2]) prints ascii value of the lowest byte of p[2], and the address of this byte is the same as address of p[2]. As sizeof(int) is most likely 4, it's the byte with an offset of 8 from the lowest byte of p[0], and lowest byte of p[0] is q[0].

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Then your gcc is using non-standard syntax rules.

                  You might try the -pedantic switch to disable non-standard features.

                  However, if you have the address of a char stored in an int* and you increment the int*, the compiler will advance sizeof(int) rather than sizeof(char) to totally screw up your memory.

                  So, if this is compiling, it is out of gross negligence to the pointer arithmetic rules.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by shadyabhi
                    Terminal Output:
                    codefire@codefi re-desktop:~$ gcc test.c
                    test.c: In function ‘main’:
                    test.c:4: warning: initialization from incompatible pointer type
                    codefire@codefi re-desktop:~$ ./a.out
                    a l
                    codefire@codefi re-desktop:~$
                    Originally posted by shadyabhi
                    thanx for reply ...
                    But, i tell you dude, that program(the exact code which i posted) compiles gcc 4.2.4...
                    Which compiler are you using??
                    gcc 4.2.4 reports warning: initialization from incompatible pointer type. It isn't any happier about line 4 than I am. True, I overstated matters when I said "it doesn't compile". You get a compiler warning, not a compiler error; but that isn't good news.

                    Comment

                    Working...