Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The input lakenum is an integer from 1-5. Given that integer, the function should assign a name and return the string array to the main function, but instead I'm getting pointer errors:

error: return makes integer from pointer without a cast
error: function returns address of local variable

I call the function using printf ("%s", lakenamer(lakenum));

int lakenamer(lakenum) {
  char lname[20] = "0";

  if (lakenum == 1) {
    strncpy(lname, "Ontario", 20);
  } else if (lakenum == 2) {
    strncpy(lname, "Erie", 20);
  } else if (lakenum == 3) {
    strncpy(lname, "Huron", 20);
  } else if (lakenum == 4) {
    strncpy(lname, "Muskoka", 20);
  } else if (lakenum == 5) {
    strncpy(lname, "Simcoe", 20);
  }

  printf("%s",lname);
  return (lname);
}
share|improve this question
    
strncpy is a bad idea, you should use strcpy or snprintf instead. –  Matt McNabb Feb 23 at 6:21
    
@ThisGuy You should accept and upvote any of the answers if they helped you. –  Anmol Singh Jaggi Feb 23 at 15:35
    
I cant up vote not enough credit, but I ended up just changing it so that the function would be called, and would print the data instead of returning the data back to the main function, it was easier –  ThisGuy Feb 24 at 13:21

4 Answers 4

You should use char *lakenamer() and return your string (or strdup(your_string)) from it.

By the way, you're using printf ("%s", lakenamer(lakenum));, so you need the return value be char * but you're returning int instead.

share|improve this answer

Also you are declaring lname as a local variable within your function, but returning a pointer to it to your calling function. This isn't good, because once you return that memory address can get written over at any time.

You could instead have the caller allocate the memory, and pass its address to you, i.e.

char[20] lname;
lakenamer(lakenum, lname);

Then inside your function you can strcpy the result into lname. Then have lakenamer return void, or int if you would like to return a success or error status code.

share|improve this answer
    
char[20] lname; --> char lname[20]; –  Matt McNabb Feb 23 at 6:13

You need to dynamically allocate the memory for the string inside the function if you want to use it outside the function.

char* lakenamer( int lakenum )
{
    char* lname = ( char* )malloc( 20 );
    if ( lakenum == 1 )
    {
        strncpy( lname, "Ontario", 20 );
    }
    else if ( lakenum == 2 )
    {
        strncpy( lname, "Erie", 20 );
    }
    else if ( lakenum == 3 )
    {
        strncpy( lname, "Huron", 20 );
    }
    else if ( lakenum == 4 )
    {
        strncpy( lname, "Muskoka", 20 );
    }
    else if ( lakenum == 5 )
    {
        strncpy( lname, "Simcoe", 20 );
    }
    printf( "%s", lname );
    return ( lname );
}

int main()
{
    int lakenum = 1;
    char* s = lakenamer( lakenum );
    printf( "%s", s );

    /*
    .
    .Do other stuff with s
    .
    */

    free( s ); // Very important !!
}
share|improve this answer

error: return makes integer from pointer without a cast

Ans: You are getting this error because you are returning address and collecting with int without cast.

error: function returns address of local variable

Ans: You are getting above error because you are returning local variable address from a function which wont be exist after returning to Main() function.

Please find the below code which solves above problem.

#include<stdio.h>
#include<string.h>

void lakenamer(int, char *);

main(int argc, char *argv[])
{
    char string[20]={0,};
    lakenamer(atoi(argv[1]), string);
    printf("\nIn Main fn:[%s]\n",string);
}

void lakenamer(int lakenum, char *str)
{
    char lname[20] = "0"; 
    //char lname[20] = {0,};

    if (lakenum == 1) {
        strncpy(lname, "Ontario", 20); //u can change lname to str
    } else if (lakenum == 2) {
        strncpy(lname, "Erie", 20);
    } else if (lakenum == 3) {
        strncpy(lname, "Huron", 20);
    } else if (lakenum == 4) {
        strncpy(lname, "Muskoka", 20);
    } else if (lakenum == 5) {
        strncpy(lname, "Simcoe", 20);
    }

    strcpy(str, lname);
    printf("%s",lname);
    //return (lname);//do not return any reference for local variable
}
share|improve this answer
    
You should explain what you are doing instead if just giving a code sample –  Matt McNabb Feb 23 at 6:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.