Thread: Need help with linker error

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Need help with linker error

    I've a C++ program to reverse a string.
    Its showing linker error. please help me debug it
    Code:
    char* st(const char *);
    
    int main()
    {
    char *str="abcdefgh", *abc="\0";
    strcpy(abc,st(str));
    printf("%s",abc);
    return 0;
    }
    
    
    char* st(const char *str[])
    {
    int n=0,i;
    while(str[n]!='\0') n++;
    char *abc="\0";
    for(i=n;i!=0;i--)
    strcat(abc,str[i]);
    return abc;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    well I suspect that your linker error is complaining about an undefined reference to "char* st(const char *)"

    you've declared the function as
    Code:
    char* st(const char *)
    but you've defined it as
    Code:
    char* st(const char *str[])
    your first problem is that you omitted the parameter name in the declaration (not an error, but bad practice, anyway). your second should be plainly visible.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    As Elkvis said,this is not an error.It is good practice,keep that in mind for future situations,to post what the compiler actually says

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Boy oh boy I didn't think one could possibly write a piece of code that short that had so many problems with it. Even fixing the linker error I'd bet it crashes.

    Anyway, if you really want to write something closer to a true C++ program and not just C code that might perchance compile using a C++ compiler you would use a std::string to store and manipulate the string. Reversing a std::string, given a source string, is a one liner:
    Code:
    std::string original("abcdefgh");                         // Source/original string
    std::string reversed(original.rbegin(),original.rend());  // Done, reversed in one line of code
    Variable reversed now contains "hgfedcba".
    Last edited by hk_mp5kpdw; 09-05-2012 at 08:31 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker error help
    By bonks in forum C Programming
    Replies: 14
    Last Post: 03-17-2011, 08:18 AM
  2. Linker error
    By Basca in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2006, 02:27 PM
  3. linker error
    By fighter92 in forum C++ Programming
    Replies: 5
    Last Post: 08-08-2006, 12:45 PM
  4. Need help with this linker error... please
    By radroach in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2005, 09:11 AM
  5. Linker Error
    By insanedonut123 in forum Windows Programming
    Replies: 2
    Last Post: 12-20-2003, 01:36 AM