Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax error..... without reason?

Status
Not open for further replies.

zanza

Programmer
Feb 18, 2002
89
US
grr... why does it tell me this?

current = head;
while(current->next != NULL)
current = current->next;
-->new = (wptr*)malloc(sizeof(struct Weapon));
current->next = new;
new->next = NULL;
current = new;

c:\projects\rpg\inventory.c(37) : error C2059: syntax error : ')'


žÅNžÅ
 
Two things zanza,

first, try calling it something else rather than 'new' - this is a reserved keyword in C/C++.

secondly, what is happening with this line highlighted in blue?:

[tt] current = head;
while(current->next != NULL)
current = current->next;
-->new = (wptr*)malloc(sizeof(struct Weapon));
current->next = new;
new->next = NULL;
current = new;[/tt]

specifically the --> you have there???
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
haha, crap on reserved key words, thank you very much

the lil --> was to show which line the complier was b*tching about žÅNžÅ
 
ok, nevermind, the keyword wasnt doing a thing.... same error same place žÅNžÅ
 
ahh I see... well, it appears that even though your compiler is pointing to that particular line, the problem area is actually above that line somewhere - possibly in code that you have not posted which has a parenthesis missing or an extra parenthesis inserted. The compiler doesn't realize that this is the case until it gets to the line (that it points out as the problem) and then it complains about it!
In other words, examine your code further up before your sample code and see if you can find a missing or extra parenthesis. :)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
the pox on visual studio... youd think that microsoft could solve the problem of error messages after the error...
*sigh*
i actually had considered that to be the problem, but i had hoped it wasnt... this will take a while, but thank you.

oh, and gednick, your deffinition of a programmer kicks... im jokingly hot-headed, i just got an anorak for christmas, im a teen (so acne i have), and im sure as hell a computer geek

with that, thanks and now to go parenthesis hunting žÅNžÅ
 
hmm... can you tell if the error is in the same file or another? this bit of code is from a separate source file from main.c ... with its own header and blah blah žÅNžÅ
 
Quick question: What is wptr? Is it

typedef struct weapon* wptr;

If it is then you have one * too many. Either drop the * or change (wptr*) to (struct weapon*)

Re braces: It is probably in the same file. Save your file somewhere else first. If you are using Visual studio, select all the code and press Alt-f8. This will reformat the code for you. Somewhere, the indentation will go wrong - that is where the missing brace is.

It is a quick way of finding it but I'm afraid it messes up your code layout.

If your code looks ok - check your local headers.

 
yeah, i just tried changing it to (struct Weapon*)... worked like a charm ^_^

but now it crashes on the while statement saying its an access violation... well, here, take a looksee

#include<stdio.h>
#include<stdlib.h>
#include&quot;functions.h&quot;
#include&quot;chargen.h&quot;
#include&quot;inventory.h&quot;

void InvenGen(void)
{
struct Weapon *New = NULL;
struct Weapon *head = NULL;
struct Weapon *current = NULL;

system(&quot;cls&quot;);

printf(&quot;Choose your primary short range weapon\n&quot;);
printf(&quot;1. Long Sword\n&quot;);
printf(&quot;2. Short Sword\n&quot;);
printf(&quot;3. Large Club\n&quot;);

current = head;
while(current->next != NULL)
current = current->next;
New = (struct Weapon *)malloc(sizeof(struct Weapon));
current->next = New;
New->next = NULL;
current = New;

system(&quot;pause&quot;);
}

im going to have it scanf the users choice to a temp variable and use that to assign the approrpriate values to the structure. that would all work wonderfully... but why does it crash on the while loop? žÅNžÅ
 
Analysis
You start with head = NULL.
Then current = head.
Therefore current->next is undefined.

Possible solution
One way around it is to make a dummy head
Code:
head = (struct Weapon*) malloc (sizeof (struct Weapon));
head->next = NULL;

 
Well, you have a Weapon* head that you set to NULL, or 0. You then assign that to another Weapon* called current. Thus current == 0. When you check current->next, you're trying to access memory that hasn't been allocated to you (somewhere past address 0).
 
Just a thought - shouldn't head be global? If it is local, you will re-create the list everytime and it will go out of scope as soon as you exit the routine.
 
thank you all very much! it works and now i can finally move on!

and xwb, this was all just figuring out linked lists as i go. i know scope, so ill do that when the time comes žÅNžÅ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top