-1

./drzwoposzukiwanbinarnych.c:84:24: error: expected expression before â)â token

char getNewSlowo(){
    slowa *wyraz = (wyraz*) malloc(sizeof(slowa)); //LINE WITH ERROR
    scanf("%s",wyraz->slowo);
    return wyraz->slowo;
}

What I am trying to do?

So, I have a struct:

typedef struct node{
char *word;
unsigned int arity;
struct node *left,*right,*parent;
}baza;

I want to make that pointer word is pointing to - char slowo[30] defined below.

typedef struct word{
    char slowo[30];
}slowa;

And the point that I am stuck on is the error on the top of this question. I am extremely tired of coding and my mind is completely overheated right now so my question may be not well formed for what I am sorry if that's the case.

But why I am trying to do this? I had a problem with assigning a word defined globally to the pointer and I noticed that when I read a new word into that global defined word the word in the struct (pointer) changed also.

Mateusz Zając
  • 316
  • 5
  • 19
  • `â)â token` implies you have some non-asci characters in your file. Check using command-line tool od? – cdarke Jan 06 '13 at 15:55

3 Answers3

2

Just remove the cast (wyraz*) and all will be fine. if you insist on keeping it (although it is unneeded and often considered detrimental), it should be (slowa *) instead.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
0

This:

slowa *wyraz = (wyraz*) malloc(sizeof(slowa));

has mis-matched pointers. It's better to write this like so:

slowa *my_slowa = malloc(sizeof *my_slowa);

This removes the pointless cast, and uses the sizeof operator to ensure the number of bytes allocated matches the type of the pointer.

Code like this is a pretty good argument for never having this cast, in my opinion.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
0

There's a reason typecasting is called type casting and not variable-name casting. What you're trying to do is using the name of the just declared variable as a type name, of course that makes no sense. If you're intending to cast away the return value of malloc(), you should use a type, and not a variable name:

slowa *wyraz = (slowa *)malloc(sizeof(slowa));

However, in C, you should not cast the return value of malloc. Furthermore, it would be less error prone if you used sizeof(*ThePointer) versus sizeof(TheType) just in case the type ever changes. All in all, write this:

slowa *wyraz = malloc(sizeof(*wyraz));
Community
  • 1
  • 1