Thread: syntactic problem

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    syntactic problem

    it is a old code . i have a syntactic problem here...



    Code:
    int n;
    char buf[81];
    char (*lines)[81];   // what is the meaning of this  syntax ?  line  -1
    
    /* fopen fin and such */
    n = 0;
    while ( fgets ( buf, sizeof buf, fin ) != NULL ) {
      n++;
    }
    lines = malloc ( n * sizeof *lines );
    rewind ( fin );
    n = 0;
    while ( fgets ( buf, sizeof buf, fin ) != NULL ) {
      strcpy ( lines[n++], buf );
    }

    look at //line -1 . i have question on char (*lines)[81]; i dont understand the meaning of this syntax. what is the meaning of this ?

    why there is a paranthesis ?


    what is the alternate version of that ?

    thanks
    blue_gene

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char (*lines)[81];
    lines is a pointer to an array of 81 chars

    char *lines[81];
    lines is an array of 81 pointers to char

    The () are important here. The first one is just one pointer, whereas the second one is an array of 81 pointers.

    What it basically allows you to do is create
    char lines[n][81];
    through the use of the malloc call later on in the code.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    lines is a pointer to an array of 81 chars
    ok.

    one question more. what is happening here....

    Code:
    lines = malloc ( n * sizeof *lines ); // how is this syntax ??


    i know sizeof(pointer) is legal. (basically gives 4 ..for 32 bit addressing)

    but sizeof(*pointer) is it also legal !!!?? thats what has been done here...... dereferencing has been done inside the sizeof operator !

    what does it mean if i write sizeof(*pointer) ?
    Last edited by blue_gene; 04-24-2004 at 05:19 AM.
    blue_gene

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    It dereferences the pointer, thus, it refers directly to the object it was previously pointing to. Thus sizeof(*pointer) will give you the size of the object it was pointing to. Eg:
    Code:
    char chChar;
    char *lpChar=&chChar;
    cout << sizeof(lpChar); //output: 4
    cout << sizeof(*lpChar); //output: 1
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    oh, ic...thanks.



    char (*lines)[81];


    can i write this alternatively like below.....

    code-1
    ---------
    char array[81];
    char* lines = &array[0] // is it same as char (*lines)[81] ?

    code-2
    --------

    char array[81];
    char *lines = array; //is it same as char (*lines)[81] ?


    can u coment plz ?

    thanks
    blue_gene

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Both of those will work. In the second example, 'array' resolves to a pointer to the actual array.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ok thanks
    blue_gene

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but sizeof(*pointer) is it also legal !!!??
    Yes. sizeof doesn't evaluate its argument, so you get the size of the object being pointed to without actually dereferencing the pointer. If the dereference were evaluated then this would be illegal code as the pointer is probably pointing into limbo at the time.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi prelude, it was yours code.... i got it from a archieve.

    Yes. sizeof doesn't evaluate its argument, so you get the size of the object being pointed to without actually dereferencing the pointer. If the dereference were evaluated then this would be illegal code as the pointer is probably pointing into limbo at the time.



    former explanation was good to understand.

    anyway, whether it really evalutes or not i understand what would be the output of that statement.

    why r u saying it does not dereference. without dereference how thing will be output ?

    the two coments are contradictory

    at least i understand the output .

    thanks for the reply
    blue_gene

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sizeof is entirely compile time (well until certain bits of C99 become common, but that's for later)
    All it does is look at the type of the expression, there it nothing to evaluate

    This leads to some quirky code if you're particularly careless...
    Code:
    #include<stdio.h>
    int main() {
        int foo = 0;
        printf( "sizeof foo %d\n", sizeof(foo++) );
        printf( "Foo is now %d\n", foo );
        return 0;
    }
    This may or may not print foo as being 1

    > char* lines = &array[0] // is it same as char (*lines)[81] ?
    No
    > char *lines = array; //is it same as char (*lines)[81] ?
    No

    You could do this, as a valid means of initialising
    char array[81];
    char (*lines)[81] = &array;

    Or use the malloc call in your original post
    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.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    This may or may not print foo as being 1
    hmmm....

    output
    ---------

    g++ t.cpp
    ./a.out

    sizeof foo 4
    Foo is now 0
    there it nothing to evaluate
    ....i see.


    char array[81];
    char (*lines)[81] = &array;
    array name itself is a pointer . for example if i write array .
    it means basically, &array[0] i.e the address of the first element of the array.

    but u r writing, char (*lines)[81] = &array; it means if i expand this mathematically it will be like this....

    char (*lines)[81] = &array;
    =>char (*lines)[81] = &(&array[0]); // just replacing

    RHS is a pointer to pointer !! LHS is a single pointer .

    sorry , for generating confusuion, but it is coming to mind. somewhere something might be wrong in my thinking.

    thanks
    blue_gene

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > RHS is a pointer to pointer !! LHS is a single pointer .
    No it isn't - what you wrote doesn't make any sense.
    &array is a distinct type from array and &array[0]

    &array is a pointer to the whole array, which happens to start at the same place as &array[0]

    Consider an alternative example using a struct.
    Code:
    #include<stdio.h>
    
    struct foo {
        int     bar;
        char    name[100];
    };
    
    int main() {
        struct foo foovar;
        struct foo *fooptr = &foovar;       /* point to the whole thing */
        int        *barptr = &foovar.bar;   /* point to a part of the whole */
        printf( "%p %p\n", (void*)fooptr, (void*)barptr );
        return 0;
    }
    fooptr is like &array
    barptr is like &array[0]


    Maybe this will help
    http://pw2.netcom.com/~tjensen/ptr/cpoint.htm
    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.

  13. #13
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi salem..i tested the code.

    output
    -----------
    g++ t.cpp
    ./a.out
    0xbfffe560 0xbfffe560 // same address

    thanks for that solid code. your answers are always to the point .....thanks for correcting me.... i have read about pointers. still, i confuse sometimes. people say, poiners are the difficult section of C/C++ programming.

    anyway, thanks.....i learned a lot.
    blue_gene

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No
    > char *lines = array; //is it same as char (*lines)[81] ?
    No
    Why isn't it the same?
    Code:
    char array[81];
    char *lines=array;
    lines is a pointer to the first of 81 sequential chars. How does it differ from char (*lines)[81]?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >char (*lines)[81]?
    This is a pointer to an array of 81 chars.
    >char *line;
    This is a pointer to a single char.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM