GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jan-2006, 10:01
vital_101 vital_101 is offline
New Member
 
Join Date: Jan 2006
Posts: 9
vital_101 is on a distinguished road

Compiler errors


I've been doing some coding in C lately, and I'm having trouble figuring out why it won't compile. I've took care of most of the compiler errors, but for the rest I'm not even sure what they mean. I'm compiling using GCC on a Sun Solaris.
My code:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define max 2048

int
main()
{
  int count = 0, n, nextval, d[max];
  void getdata(int d[], char *n);

  getdata(int d[max], char *n);
  printf("The value in place 4 is %d\n", d[3]);
  exit(0);
}
 
void 
getdata(int d[], char *n) 
{
  int count, nextval; 
  while(count < max && (n = scanf("%d", &nextval)) != EOF) {
      d[count++] = nextval;
    }
}

My compiler errors:

Quote:
test.c: IN function 'main':
test.c:11: error: parse error before "int"
test.c: In function 'getdata':
test.c:20: warning: assignment makes pointer from integer without a cast
test.c:20: warning: comparison between pointer and integer


Thanks for any help.
/vital_101
  #2  
Old 12-Jan-2006, 10:38
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: Compiler errors


See the changed code...it can be bettered in many ways...just made it work for the moment

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#define max 5

void getdata(int d[]);

int main()
{
  int nextval, d[max];
  getdata(d);
  printf("The value in place 4 is %d\n", d[3]);
  exit(0);
}
 
void getdata(int d[]) 
{
  int count=0, nextval; 
  int n;
  while(count < max && (n = scanf("%d", &nextval)) != EOF) {
      d[count] = nextval;
	  count++;
    }
}
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
Last edited by LuciWiz : 13-Jan-2006 at 01:08. Reason: Please insert your C code between [c] & [/c] tags
  #3  
Old 12-Jan-2006, 11:05
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Compiler errors


Quote:
while(count < max && (n = scanf("%d", &nextval)) != EOF) {
I dunno C, never used it, but I know C++ so think I should be able t help you with one of those errors. The mistake in the above code fragment is that u a have declared n as "int *n", what this does is declare n as a pointer to an integer variable; that is it can store the "memorey addres" of an integer variable and not any int "value".

The correct way would be:
CPP / C++ / C Code:
int *n;
int a=10;
n=&a; //&=>"address of" operator
Here, n stores the address of a (which is in a hexadecimal format I think), n can be called an alias (Senior members tell me if i'm mistaken) of the variable a. And what u end up comparing in the while() loop is an "address" of an integer and an integer value.
Think over it. I'm too sleepy right now, to think anymore.

Logging off....

Saving your settings......

My Mind is shutting down......

[Blup]

Netnut.
  #4  
Old 12-Jan-2006, 13:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Compiler errors


Because when calling a function:
CPP / C++ / C Code:
getdata(int d[max], char *n);
you don't specify the function definition, you specify the function parameters. Change it to:
CPP / C++ / C Code:
getdata(d, &n);
d - passing the address of the array
n - pass the address of the variable



My personal preference for the function is:
CPP / C++ / C Code:
void 
char getdata(int d[]) 
{
   int count, nextval; 
   while(count < max && (n = scanf("%d", &nextval)) != EOF) 
   {
      d[count++] = nextval;
   }
   return (n);
}
I dislike void functions passing back values via the parameter list. They are harder to debug and less flexible.

Also, I question whether your n has a useful value. It returns 1 if there are more than count values entered, and 0 when there are fewer.
__________________

Age is unimportant -- except in cheese
  #5  
Old 12-Jan-2006, 13:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiler errors


Quote:
Originally Posted by WaltP
[/c]
I dislike void functions passing back values via the parameter list. They are harder to debug and less flexible.


An excellent point of style for functions that return only one thing.

Quote:
Originally Posted by WaltP
Also, I question whether your n has a useful value.

n is equal to the last value returned by scanf() before control left the loop: It could be 0; it could be 1; it could be EOF. Anyhow, like WaltP, I don't see the usefulness of having the function return the value of n.

Maybe it should return count, since there is no other way for the calling program to know how many items were successfully scanned. (And, of course, that means that count should be initialized to at the beginning of the function.)

Regards,

Dave
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
Today's C/C++ Programming Tip :-) Paramesh C Programming Language 6 10-Feb-2007 23:46
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Help with compiler errors Krandygrl00 C++ Forum 3 01-Jun-2005 07:45
Newbie : need help with Dev-C++ compiler batrsau C++ Forum 2 20-Mar-2005 21:05
compiler errors Newworld C Programming Language 1 28-Oct-2004 07:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.