Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 30th, 2008, 1:36 PM   #1
hipomandra
Newbie
 
Join Date: Sep 2008
Posts: 4
Rep Power: 0 hipomandra is on a distinguished road
Help with syntax error in C

Ok so I'm currently taking a course on C and for one of my assignments I am getting these compile time warnings/errors:
Exercise1.c: In function `main':
Exercise1.c:16: warning: passing arg 1 of `atol' from incompatible pointer type
Exercise1.c:17: warning: passing arg 1 of `atoi' from incompatible pointer type
Exercise1.c:19: warning: passing arg 1 of `sprintf' from incompatible pointer type
Exercise1.c:20: warning: passing arg 1 of `strcpy' from incompatible pointer type
Exercise1.c:27: warning: passing arg 1 of `atoi' from incompatible pointer type
Exercise1.c:34: warning: passing arg 1 of `strcmp' from incompatible pointer type
Exercise1.c:35: warning: passing arg 1 of `atoi' from incompatible pointer type
Exercise1.c:40: warning: passing arg 1 of `atoi' from incompatible pointer type
Exercise1.c:41: error: syntax error before "else"
Exercise1.c: At top level:
Exercise1.c:53: error: syntax error before '}' token

I have been using Java for 3 years now and have just started C. Can anyone help me find these two syntax errors and explain them for me as I cannot seem to find them myself.
Thanks, Andrew

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int main(){
  7. char *string[101], *grade[3];
  8. char *studentID[9];
  9. char *firstName[50], *lastName[50], *finalExam[4], *midterm[3], *quiz1[3], *quiz2[3], *quiz3[3];
  10.  
  11.  
  12.  
  13. while(!feof(stdin)){
  14. scanf("%s %s %s %s %s %s %s %s", studentID, firstName, lastName, finalExam, midterm, quiz1, quiz2, quiz3);
  15.  
  16. long int id = atol(studentID);
  17. int mark = atoi(finalExam);
  18.  
  19. if(sprintf(studentID, "%ld", id) != 8){
  20. strcpy(studentID,"XXXXXXXX");
  21. }
  22.  
  23.  
  24. if((mark > 100) || (mark < 0)){
  25. printf("%s %s %s ILLEGAL MARK", studentID, firstName, lastName);
  26. }
  27.  
  28. mark = atoi(midterm);
  29. if((mark > 50) || (mark < 0)){
  30. printf("%s %s %s ILLEGAL MARK", studentID, firstName, lastName);
  31. }
  32.  
  33.  
  34. double quizPercent = 0.0;
  35. if(strcmp(quiz1,"-") == 0){
  36. mark = atoi(quiz2);
  37. if((mark > 10) || (mark < 0)){
  38. printf("%s %s %s ILLEGAL MARK", studentID, firstName, lastName);
  39. }
  40.  
  41. mark = atoi(quiz3);
  42. else if((mark > 10) || (mark < 0)){
  43. printf("%s %s %s ILLEGAL MARK", studentID, firstName, lastName);
  44. }
  45.  
  46. else{
  47. quizPercent = atod(quiz2) + atod(quiz3);
  48. quizPercent /= 20.0;
  49. }
  50. }
  51.  
  52. printf("%s\n",studentID);
  53. }
  54. }
hipomandra is offline   Reply With Quote
Old Sep 30th, 2008, 2:25 PM   #2
MasterWill
C/C++ Developer
 
MasterWill's Avatar
 
Join Date: Sep 2008
Location: Florida, USA
Posts: 43
Rep Power: 0 MasterWill is on a distinguished road
Re: Help with syntax error in C

It is in your declarations for the character buffers ... if you want to allocate space for a null-terminated string, you would do it this way:

/* You have this ... */
char *myString[80];

/* ... that allocates a buffer for 80 char* items ... what you want is ... */

char myString[80]; /* this is an 80-char buffer for a string */

Essentially, you have declared a collection of string pointers, not a string itself.


HTH
__________________
-- William
MasterWill is offline   Reply With Quote
Old Sep 30th, 2008, 2:27 PM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Help with syntax error in C

Quote:
Originally Posted by hipomandra View Post
c Syntax (Toggle Plain Text)
  1. int main(){
  2. char *studentID[9];
  3. long int id = atol(studentID);
What I see is on this one, you set up a pointer to a char called studentID. When you assign this to a long, the compiler is telling you you can't assign a pointer to a long. You need set up your studentID as a char, then assign a pointer to your char studentID if you want to use a pointer to access it and dereference your pointer when assigning values.
Jabo is offline   Reply With Quote
Old Sep 30th, 2008, 2:46 PM   #4
hipomandra
Newbie
 
Join Date: Sep 2008
Posts: 4
Rep Power: 0 hipomandra is on a distinguished road
Re: Help with syntax error in C

Quote:
Originally Posted by Jabo View Post
What I see is on this one, you set up a pointer to a char called studentID. When you assign this to a long, the compiler is telling you you can't assign a pointer to a long. You need set up your studentID as a char, then assign a pointer to your char studentID if you want to use a pointer to access it and dereference your pointer when assigning values.
Jabo, I believe you are talking about line 16 in which I am using the function atol(const char *someStringPointer) to assign a string pointer to a long. The warning is because studentID is not a constant string pointer not due to the fact that it is a pointer. That code does work as I tried it on an earlier version of the code which did not contain lines 24 - 49.

The warnings being issued are can be bypassed and the program will ruin after issuing these warnings. The errors I am talking about are for lines 41 & 54 (the error says 53 & 40 but I ended up adding an extra blank line to make the code more legible.

Thank you for your response Jabo.

Quote:
Originally Posted by MasterWill View Post
It is in your declarations for the character buffers ... if you want to allocate space for a null-terminated string, you would do it this way:

/* You have this ... */
char *myString[80];

/* ... that allocates a buffer for 80 char* items ... what you want is ... */

char myString[80]; /* this is an 80-char buffer for a string */
Essentially, you have declared a collection of string pointers, not a string itself.


HTH

MasterWill I ended up changing char *string[101] to char myString[101] creating a string buffer however I am still getting these same errors.
hipomandra is offline   Reply With Quote
Old Sep 30th, 2008, 2:48 PM   #5
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Help with syntax error in C

What MaterWill suggested must be applied to every string, not just his particular example.

A C-string is an array of characters. Not an array of character pointers. Do some googling for how to use strings in C.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Sep 30th, 2008, 3:15 PM   #6
hipomandra
Newbie
 
Join Date: Sep 2008
Posts: 4
Rep Power: 0 hipomandra is on a distinguished road
Re: Help with syntax error in C

Thanks Sane, I'll definitely look up how to use strings better in C.
The advice took away all the warnings however I am still getting these compile time errors.

Exercise1_palmer.c: In function `main':
Exercise1_palmer.c:42: error: syntax error before "else"
Exercise1_palmer.c: At top level:
Exercise1_palmer.c:54: error: syntax error before '}' token
hipomandra is offline   Reply With Quote
Old Sep 30th, 2008, 3:21 PM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
Re: Help with syntax error in C

the else if on line 42 is not proceeding after an if statement. you need to do something about line 41.
Seif is offline   Reply With Quote
Old Sep 30th, 2008, 3:39 PM   #8
hipomandra
Newbie
 
Join Date: Sep 2008
Posts: 4
Rep Power: 0 hipomandra is on a distinguished road
Re: Help with syntax error in C

Quote:
Originally Posted by Seif View Post
the else if on line 42 is not proceeding after an if statement. you need to do something about line 41.
Wow I can't believe I missed that lol...such a simple mistake. Thanks Seif.

I am currently having no more errors with my program . Thanks for all the help!
hipomandra is offline   Reply With Quote
Old Oct 2nd, 2008, 5:47 PM   #9
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 105
Rep Power: 0 null_ptr0 is an unknown quantity at this point
Re: Help with syntax error in C

you're passing char ** when you meant char *. change the char *str[len] to char str[len]

also,
mark = atoi(quiz3);
is placed before the 'else' so its being read as:

if (...) { .... } mark = atoi(quiz13); else if (...) { .... }

remove the 'else' statement because it is not what you meant
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''''' at line 1 [STOPED] jackparsons PHP 3 Aug 21st, 2008 12:59 AM
syntax error in Mysql... ktsirig PHP 1 Feb 3rd, 2007 2:30 PM
Header file internal errors kruptof Coder's Corner Lounge 2 Jan 14th, 2007 2:12 PM
Which test syntax to use? aznluvsmc Bash / Shell Scripting 4 Jan 17th, 2006 7:00 PM
From C syntax to C++ syntax Navid C++ 13 Jan 15th, 2006 8:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC