Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Arrays initialization and declaration


    why is this wrong to declare arrays and then initialize it
    For instance

    int x;
    x =10;

    the above code is valid.
    but the below code raises an error
    char searchstring[] = {"am not a fool"};
    char tracks[10];
    tracks ={"am not ready"};
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    char searchstring[] = {"am not a fool"};
    This is initialisation, which you can do for an array.

    > char tracks[10];
    > tracks ={"am not ready"};
    This is assignment, which you can't do for arrays.

    Use instead
    strcpy(tracks,"am not ready"); //!! needs 13 chars, not 10

    Making sure of course that the string will fit.

    Comments on this post

    • Technovicez agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    Thanks alot
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    I see, by experiment, that what you say is true. Isn't this a hole in the c language definition?

    "abd" is a pointer.
    { } initialization is an array.
    {"abc"} should be an array of pointers.

    How c handles this depends on the declaration:

    Code:
    #if 0
    
      display from program run:
    
      a    %p  0x601030
      a[0] %c  
      b[0] %c  a
    
    #endif
    
    #include<stdio.h>
    char*a[] = {"am not a fool"};
    char b[] = {"am not a fool"};
    int main() {
      printf("a    %%p  %p\n",a);
      printf("a[0] %%c  %c\n",a[0]);/* format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat] */
      printf("b[0] %%c  %c\n",b[0]);
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I see, by experiment, that what you say is true. Isn't this a hole in the c language definition?
    Not if it's been defined that way. The 'holes' in C are called undefined behavior.

    char*a[] = {"am not a fool"};
    a points to a pointer containing the string.
    a is also an array of pointers.

    char b[] = {"am not a fool"};
    b points to the string.

    printf("a %%p %p\n",a);
    Display the pointer a points to.

    printf("a[0] %%c %c\n",a[0]);
    Display the 1st byte of the 1st pointer as a character.

    printf("b[0] %%c %c\n",b[0]);
    Display the first byte of the string as a character.

    So what's the problem? ;)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    Clearly the behavior is defined, and I wasn't included on the c standards committee. My misunderstanding was on the right hand side, not on the left hand side.

    In the initializer
    {"am not a fool"}
    The curly braces indicate an array. That's one level of indirection.

    The "string" represents a pointer. That's a second level of indirection.

    thus, while
    char b[] = {"am not a fool"};

    might be unambiguous and supported, it looks wrong.

    Furthermore, as you say,
    char b[] =
    b points to the string.
    b is an array.
    a string is a pointer.

    I wouldn't write code like that, intentionally.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Clearly the behavior is defined, and I wasn't included on the c standards committee. My misunderstanding was on the right hand side, not on the left hand side.

    In the initializer
    {"am not a fool"}
    The curly braces indicate an array. That's one level of indirection.
    They do? I thought they simply indicated a group. What's inside might be an array... Braces do not indicate an array.


    Originally Posted by b49P23TIvg
    The "string" represents a pointer. That's a second level of indirection.
    Yep. And nope. It's still just a string.

    Originally Posted by b49P23TIvg
    thus, while
    char b[] = {"am not a fool"};

    might be unambiguous and supported, it looks wrong.
    It's the same as
    char b[] = "am not a fool";

    What's the difference between:
    Code:
    if (i == 1) b = 25;
    Code:
    if (i == 1) 
    {
        b = 25;
    }
    One has curly braces. It must be different according to your logic.

    Originally Posted by b49P23TIvg
    Furthermore, as you say,

    b is an array.
    a string is a pointer.

    I wouldn't write code like that, intentionally.
    Fine. Then don't. You need to write code to your own style. Just understand that others may have a different style and you need to be able to distinguish correct and incorrect syntax in either style.

    For example, my style for an IF is:
    Code:
    if (i == 1) 
    {
        b = 25;
    }
    I hate this format:
    Code:
    if (i == 1) {
        b = 25;
    }
    and despise this format:
    Code:
    if (i == 1) 
        {
        b = 25;
        }
    All are acceptable conventions and I cannot tell the other programmers they are wrong. I just have to live with it. Under my breath, though... :chomp:
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    Mr.waltp AM a newbie to this language and i just started experimenting.

    But still this point is not clear

    par with my instance

    i.e
    int x;
    x= 20;
    /*if above code is correct */
    then

    char search[40] ;
    search = {"am sid"};

    /*why the above code is raising compilation erros*/

    Please do explain me in terms of memory also.....
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    [QUOTE=salem]char searchstring[] = {"am not a fool"};
    This is initialisation, which you can do for an array.

    > char tracks[10];
    > tracks ={"am not ready"};
    This is assignment, which you can't do for arrays.

    sir if we declare an array and take input from stdin then how itz able to assign our value to array .if its possible in that case then why itz not possible in the above code.
  18. #10
  19. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Originally Posted by Technovicez
    sir if we declare an array and take input from stdin then how itz able to assign our value to array .if its possible in that case then why itz not possible in the above code.
    I already showed you - use strcpy()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    To WaltP:
    I recommend you run a pretty printer on c code with your favorite settings. Then the format of the code doesn't matter.

    For instance, bcpp
    which I originally learned as
    cb

    Dave.
    [code]Code tags[/code] are essential for python code and Makefiles!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    Originally Posted by b49P23TIvg
    To WaltP:
    I recommend you run a pretty printer on c code with your favorite settings.
    Why?
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    With a pretty printer you'd transform your hate into love.
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by Technovicez
    int x;
    x= 20;
    /*if above code is correct */
    then

    char search[40] ;
    search = {"am sid"};

    /*why the above code is raising compilation erros*/
    Because in the 1st code x is an int.
    It can be assigned to, among other things.

    On the other hand, in the 2nd code search is an array.
    Arrays, in C, cannot be assigned to (they can be initialized).

    Why can't arrays be assign to, you ask.

    ... Well ... arrays are, in C, second class citizens. They only exist as arrays in a few places (definition, arguments to sizeof, and arguments to unary & operator). In all other places, they get automatically converted to a pointer to their first element. So, the assignment in the 2nd code is (would be if it were legal) converted to
    Code:
    &search[0] = {"am sid"};
    and, obviously, the address of the first letter in the string cannot be changed.

    Comments on this post

    • Technovicez agrees : thankz a ton ,clear explanation
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    Originally Posted by b49P23TIvg
    With a pretty printer you'd transform your hate into love.
    As I said, I can live with it.

    And if I couldn't, why use someone else's program? I'd just write my own that does exactly what I want... ;)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo