Error in function declaration

N

nick048

Hi
In the main I have declared a variable
char string[100];

I need to construct this variable with a function and I have written
the code:

char stringReceived(int sockDesc){
char unsigned c;
char tmp[100] = " ";
int i=0;
int n;

do {
n = recv(sockDesc, &c, 1, 0); // this function exist for
client/server dialog and is not rilevant for my problem
if (c!='\n'){
tmp=c;
i++;}
} while( c!='\n' );
tmp='\0';
return(tmp);
}

In the main I call:
string = stringReceived(nSocketDesc);

When I compile the program, this error is returned:
incompatible types assignement in function stringReceived and the line
number is the call.

Sure I mistake the declaration of function, but i don't be able to
correct the problem.

Can someone help me ?
Best Regards
Nick
 
J

Jonas

nick048 said:
Hi
In the main I have declared a variable
char string[100];

I need to construct this variable with a function and I have written
the code:

char stringReceived(int sockDesc){
char unsigned c;
char tmp[100] = " ";
int i=0;
int n;

do {
n = recv(sockDesc, &c, 1, 0); // this function exist for
client/server dialog and is not rilevant for my problem
if (c!='\n'){
tmp=c;
i++;}
} while( c!='\n' );
tmp='\0';
return(tmp);
}

In the main I call:
string = stringReceived(nSocketDesc);

When I compile the program, this error is returned:
incompatible types assignement in function stringReceived and the line
number is the call.


Your function is returning a pointer value, but is declared as a function
returning char. Changing to

char * stringReceived(int sockDesc)

still wouldn't work, however, since you are returning a pointer to an object
with automatic storage duration. A better approach would be to have the
buffer be a parameter of the function:

void stringReceived(int sockDesc, char * buffer);


Better still, give the length of the buffer too, and let the function return
a true or false value depending on successful completion:

int stringReceived(int sockDesc, char * buffer , size_t len);


In the function, write to buffer instead of tmp. Call like so:

if (!stringReceived (nSocketDesc, string, 100)) {
/* handle error here */
}
 
F

Fred Kleinschmidt

nick048 said:
Hi
In the main I have declared a variable
char string[100];

I need to construct this variable with a function and I have written
the code:

char stringReceived(int sockDesc){

Here you declare that stringReceived returns a value of type char
char unsigned c;
char tmp[100] = " ";
int i=0;
int n;

do {
n = recv(sockDesc, &c, 1, 0); // this function exist for
client/server dialog and is not rilevant for my problem
if (c!='\n'){
tmp=c;
i++;}
} while( c!='\n' );
tmp='\0';
return(tmp);


Here you are trying to return a variable of type char[100].
This is not legal. You cannot return an array.
}

In the main I call:
string = stringReceived(nSocketDesc);

When I compile the program, this error is returned:
incompatible types assignement in function stringReceived and the line
number is the call.

Sure I mistake the declaration of function, but i don't be able to
correct the problem.

Can someone help me ?

Either pass the array into stringReceived:
void stringReceived( int sockDesc, char *mystring, int lengthofstring )

or make the array in stringReceived static and return a pointer to it:
char * stringReceived( int sockDesc) {
static char tmp[100];
char *ptr = tmp;
...
return ptr;
}
Also think about allocating space instead of using a fixed length array.
What happens if there are more then 99 characters?
 
B

Barry Schwarz

Hi
In the main I have declared a variable
char string[100];

I need to construct this variable with a function and I have written
the code:

char stringReceived(int sockDesc){
char unsigned c;
char tmp[100] = " ";
int i=0;
int n;

do {
n = recv(sockDesc, &c, 1, 0); // this function exist for
client/server dialog and is not rilevant for my problem
if (c!='\n'){
tmp=c;
i++;}
} while( c!='\n' );
tmp='\0';
return(tmp);


return is a statement, not a function. The parentheses are
superfluous.

In this context, the expression tmp will evaluate to the address of
tmp[0] with type char*.

1 - This is inconsistent with the return type specified at the
start of the function definition.

2 - Even if you change the function's return type, tmp will cease
to exist as soon as the function exits. While it is not illegal to
return the value of a no-longer-existing object, it is illegal for the
calling function to do anything with that address. Technically, the
address becomes indeterminate.
}

In the main I call:
string = stringReceived(nSocketDesc);

If you really defined string as an array as you claimed at the
beginning of the post, this would require a diagnostic since an array
name cannot appear on the left of the assignment operator.

Maybe you would like to show us the real code.
When I compile the program, this error is returned:
incompatible types assignement in function stringReceived and the line
number is the call.

Sure I mistake the declaration of function, but i don't be able to
correct the problem.


Remove del for email
 
C

CBFalconer

nick048 said:
In the main I have declared a variable
char string[100];

I need to construct this variable with a function and I have written
the code:

char stringReceived(int sockDesc){
char unsigned c;
char tmp[100] = " ";
int i=0;
int n;

do {
n = recv(sockDesc, &c, 1, 0); // this function exist for
client/server dialog and is not rilevant for my problem
if (c!='\n'){
tmp=c;
i++;}
} while( c!='\n' );
tmp='\0';
return(tmp);
}

In the main I call:
string = stringReceived(nSocketDesc);


Declare c as a char *, malloc the space in the function, and then
return c. As it is you are returning a pointer to space you no
longer own.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top