"test.c:20: error: expected identifier before =?UTF-8?B?4oCYKOKAmQ==?= token"

Discussion in 'C Programming' started by ramif_47, Nov 5, 2007.

  1. ramif_47

    ramif_47 Guest

    Hi,

    I'm trying to create a buffer (struct) that holds an array of characters.

    struct bufferType
    {
    char *data; //array of characters -- not string
    };

    The buffer is initialized without any problems, but when I try to store a
    character in the first location (of data), the following error appears:

    "test.c:20: error: expected identifier before ‘(’ token"


    Can any one figure out what does this error means?
    BTW here is my code:


    #include <stdio.h>
    #include <stdlib.h>


    struct bufferType
    {
    char *data; //array of characters -- not string
    };

    typedef struct bufferType buffer;



    main()
    {
    buffer *buf = malloc(sizeof(buffer));

    buf->data = malloc(100 * sizeof(char));

    buf->(*(data+0)) = 'a'; //here is where error occurs

    return 0;
    }
     
    ramif_47, Nov 5, 2007
    #1
    1. Advertising

  2. ramif_47

    santosh Guest

    Re: "test.c:20: error: expected identifier before ?(? token"

    On Monday 05 Nov 2007 4:22 pm ramif_47 <> wrote in
    article <fgmsed$50h$>:

    > Hi,
    >
    > I'm trying to create a buffer (struct) that holds an array of
    > characters.
    >
    > struct bufferType
    > {
    > char *data; //array of characters -- not string
    > };
    >
    > The buffer is initialized without any problems, but when I try to
    > store a character in the first location (of data), the following error
    > appears:
    >
    > "test.c:20: error: expected identifier before ?(? token"
    >
    >
    > Can any one figure out what does this error means?
    > BTW here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    > struct bufferType
    > {
    > char *data; //array of characters -- not string
    > };
    >
    > typedef struct bufferType buffer;
    >
    >
    >
    > main()
    > {
    > buffer *buf = malloc(sizeof(buffer));
    >
    > buf->data = malloc(100 * sizeof(char));


    sizeof(char) is by definition one.

    > buf->(*(data+0)) = 'a'; //here is where error occurs


    Do:
    buf->data[0] = 'a';

    or

    (buf->data+0) = 'a';

    >
    > return 0;


    Declare main as returning an int. Implicit int is not legal anymore.

    Don't forget to free allocated storage.

    > }
     
    santosh, Nov 5, 2007
    #2
    1. Advertising

  3. ramif_47

    Chris Dollin Guest

    Specifically /not/ mailed:

    ramif_47 wrote:

    > Hi,
    >
    > I'm trying to create a buffer (struct) that holds an array of characters.
    >
    > struct bufferType
    > {
    > char *data; //array of characters -- not string


    That's /not/ an array of characters; that's a pointer. Pointers are not
    arrays, in much the same way that my head isn't my hat.

    > };
    >
    > The buffer is initialized without any problems, but when I try to store a
    > character in the first location (of data), the following error appears:
    >
    > "test.c:20: error: expected identifier before ‘(’ token"


    (fx:snip)

    > buf->(*(data+0)) = 'a'; //here is where error occurs


    After a `->` (or a `.`), a (member) name is required. What you meant to
    write was:

    *(buf->data + 0) = 'a';

    although the form:

    buf->data[0] = 'a';

    is the neatest I can think of.

    --
    Chris "[0], the postfix indirection operator" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 5, 2007
    #3
  4. ramif_47

    Mark Bluemel Guest

    =?UTF-8?B?UmU6ICJ0ZXN0LmM6MjA6IGVycm9yOiBleHBlY3RlZCBpZGVudGlmaWU=?==?UTF-8?B?ciBiZWZvcmUg4oCYKOKAmSB0b2tlbiI=?=

    ramif_47 wrote:
    > Hi,
    >
    > I'm trying to create a buffer (struct) that holds an array of characters.
    >
    > The buffer is initialized without any problems, but when I try to store a
    > character in the first location (of data), the following error appears:
    >
    > "test.c:20: error: expected identifier before ‘(’ token"
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > struct bufferType
    > {
    > char *data; //array of characters -- not string
    > };
    >
    > typedef struct bufferType buffer;
    > main()
    > {
    > buffer *buf = malloc(sizeof(buffer));
    > buf->data = malloc(100 * sizeof(char));
    > buf->(*(data+0)) = 'a'; //here is where error occurs


    You can't dereference that way. Nor can you add 0 to "data" ("data" only
    exists in an instance of a "struct bufferType").

    *buf->data would work... (adding 0 seems a little pointless)
    [I would probably use *(buf->data) for clarity]

    > return 0;
    > }
     
    Mark Bluemel, Nov 5, 2007
    #4
  5. ramif_47

    Mark Bluemel Guest

    Re: "test.c:20: error: expected identifier before ?(? token"

    santosh wrote:
    > On Monday 05 Nov 2007 4:22 pm ramif_47 <> wrote in
    > article <fgmsed$50h$>:


    >> buf->(*(data+0)) = 'a'; //here is where error occurs

    >
    > Do:
    > buf->data[0] = 'a';
    >
    > or
    >
    > (buf->data+0) = 'a';


    I think you missed a '*' somewhere...
     
    Mark Bluemel, Nov 5, 2007
    #5
  6. ramif_47

    pete Guest

    Re: "test.c:20: error: expected identifier before ?(? token"

    santosh wrote:
    >
    > On Monday 05 Nov 2007 4:22 pm ramif_47 <> wrote in
    > article <fgmsed$50h$>:
    >
    > > Hi,
    > >
    > > I'm trying to create a buffer (struct) that holds an array of
    > > characters.
    > >
    > > struct bufferType
    > > {
    > > char *data; //array of characters -- not string
    > > };
    > >
    > > The buffer is initialized without any problems, but when I try to
    > > store a character in the first location (of data),
    > > the following error appears:
    > >
    > > "test.c:20: error: expected identifier before ?(? token"
    > >
    > >
    > > Can any one figure out what does this error means?
    > > BTW here is my code:
    > >
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > >
    > >
    > > struct bufferType
    > > {
    > > char *data; //array of characters -- not string
    > > };
    > >
    > > typedef struct bufferType buffer;
    > >
    > >
    > >
    > > main()
    > > {
    > > buffer *buf = malloc(sizeof(buffer));
    > >
    > > buf->data = malloc(100 * sizeof(char));

    >
    > sizeof(char) is by definition one.
    >
    > > buf->(*(data+0)) = 'a'; //here is where error occurs

    >
    > Do:
    > buf->data[0] = 'a';


    That's OK.

    >
    > or
    >
    > (buf->data+0) = 'a';


    That's wrong.

    *(buf->data) = 'a';

    >
    > >
    > > return 0;

    >
    > Declare main as returning an int. Implicit int is not legal anymore.
    >
    > Don't forget to free allocated storage.


    Also don't forget to check that allocation is successful.

    --
    pete
     
    pete, Nov 5, 2007
    #6
  7. ramif_47

    santosh Guest

    Re: "test.c:20: error: expected identifier before ?(? token"

    On Monday 05 Nov 2007 4:44 pm Mark Bluemel <>
    wrote in article <fgmtud$a48$>:

    > santosh wrote:
    >> On Monday 05 Nov 2007 4:22 pm ramif_47 <> wrote
    >> in article <fgmsed$50h$>:

    >
    >>> buf->(*(data+0)) = 'a'; //here is where error occurs

    >>
    >> Do:
    >> buf->data[0] = 'a';
    >>
    >> or
    >>
    >> (buf->data+0) = 'a';

    >
    > I think you missed a '*' somewhere...


    Oops yes. I meant *(buf->data+0). Thanks (also to pete).
     
    santosh, Nov 5, 2007
    #7
  8. ramif_47

    ramif_47 Guest

    Re: "test.c:20: error: expected identifier before ?(? token"

    santosh wrote:

    > On Monday 05 Nov 2007 4:22 pm ramif_47 <> wrote in
    > article <fgmsed$50h$>:
    >
    >> Hi,
    >>
    >> I'm trying to create a buffer (struct) that holds an array of
    >> characters.
    >>
    >> struct bufferType
    >> {
    >> char *data; //array of characters -- not string
    >> };
    >>
    >> The buffer is initialized without any problems, but when I try to
    >> store a character in the first location (of data), the following error
    >> appears:
    >>
    >> "test.c:20: error: expected identifier before ?(? token"
    >>
    >>
    >> Can any one figure out what does this error means?
    >> BTW here is my code:
    >>
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >>
    >> struct bufferType
    >> {
    >> char *data; //array of characters -- not string
    >> };
    >>
    >> typedef struct bufferType buffer;
    >>
    >>
    >>
    >> main()
    >> {
    >> buffer *buf = malloc(sizeof(buffer));
    >>
    >> buf->data = malloc(100 * sizeof(char));

    >
    > sizeof(char) is by definition one.
    >
    >> buf->(*(data+0)) = 'a'; //here is where error occurs

    >
    > Do:
    > buf->data[0] = 'a';
    >
    > or
    >
    > (buf->data+0) = 'a';
    >
    >>
    >> return 0;

    >
    > Declare main as returning an int. Implicit int is not legal anymore.
    >
    > Don't forget to free allocated storage.
    >
    >> }


    Thank to all!
     
    ramif_47, Nov 5, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page