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

R

ramif_47

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;
}
 
S

santosh

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.
 
C

Chris Dollin

Specifically /not/ mailed:

ramif_47 said:
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.
 
M

Mark Bluemel

ramif_47 said:
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]
 
P

pete

santosh said:
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';
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.
 
R

ramif_47

santosh said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top