Thread: Error in function declaration

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Error in function declaration

    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:

    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);
    }
    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 probleni.
    Can someone help me ?
    Best Regards
    Nick

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You need to return a char* because the return value is a string, not a single char.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by Noir View Post
    You need to return a char* because the return value is a string, not a single char.
    Please,Must I to modify my function in this way?
    Code:
    char* stringReceived(int sockDesc){... }
    I have tried, but without result.

    Bye

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yeah, that's how you do it. You say you've tried, but I can't help if you don't describe the result.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by Noir View Post
    Yeah, that's how you do it. You say you've tried, but I can't help if you don't describe the result.
    I have solved.
    My previous function return only a char.
    The new solution is :
    Code:
     
    void stringReceived(int sockDesc,char &s){
     char unsigned c;
     int i=0;
     int n;
     do {
           n = recv(sockDesc, &c, 1, 0);
           if (c!='\n'){
              s[i]=c;
              i++;}
          } while( c!='\n' );
         s[i]='\0';     
    }
    and the call is:
    stringReceived(nSocketDesc,myString);
    myString is passed for reference.

    Thank You and Best Regards
    Nick

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are no references in C. You are using C++ instead of C now. Furthermore, you're doing that wrong too. s is a single character, not a string. Your code is broken.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM