Re: Error in function declaration



On 11 Apr 2007 07:02:27 -0700, "nick048" <nicosia.gaetano@xxxxxxxxxxx>
wrote:

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[i]=c;
i++;}
} while( c!='\n' );
tmp[i]='\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
.



Relevant Pages

  • Re: MFC dll and exe porting to 64-bit
    ... Why the obsolete 'char *' instead of the proper LPTSTR? ... We are no longer programming 16-bit windows in an 8-bit character world, ... string arguments should probably be LPCTSTR, that is, const parameters. ... declaration of the function itself, in context (meaning show the surrounding class ...
    (microsoft.public.vc.mfc)
  • Re: malloc warning gcc > 4.0
    ... > char* p; ... > warning: incompatible implicit declaration of built-in function 'malloc' ...
    (comp.lang.c.moderated)
  • Re: Banks and economy
    ... char cptr; ... I'm assuming that it's at file scope, because otherwise the array declaration wouldn't be allowed. ... In some cases, a tentative definition with no corresponding external definition ends up being treated as the actual definition, with an implicit initializer of 0. ...
    (comp.lang.c)
  • Re: segmentation fault
    ... char s; ... it serves to temporarily replace the declaration ... While fgets returns a null string, ... You shouldn't try to printf a NULL pointer. ...
    (comp.lang.c)
  • Re: Access one character in an array of characters
    ... enum Suit; ... Without seeing the exact declaration of ClubArray I cannot be certain, ... but you have probably declared it as a C-style string, ... confusion between a string and a vector of char. ...
    (alt.comp.lang.learn.c-cpp)