Thread: why does this code do something diff on 2 diff boxes?

  1. #1
    Registered User
    Join Date
    Feb 2005

    why does this code do something diff on 2 diff boxes?

    I'm learning C via Sam's Teach Yourself C for Linux Programming in 21 Years...
    I mean Days.

    I typed out the below code on my laptop and compiled it three times with GCC with the following command $gcc -Wall sort.c -o sort.
    This error came back each time (sort of):
    /home/casper/tmp/ccox4j2H.o(.text+0x62): In function `main':
    : undefined reference to `print_strings'
    collect2: ld returned 1 exit status

    My working directory was /home/casper/CStuff/lessons

    I looked in /home/casper/tmp using $ls -a and could find no file called ccox4jH.o(.text+0x62)

    I went back over the code three times and could find nothing different between what I had typed out and what was in the book.
    Each time I recompiled the sort.c document the value at the end of the error
    string /home/casper/tmp showed a different string- though the format and the remainder of the string appeared the same.

    I recreated the sort.c file on my desktop and compiled it. It worked and did
    what the book said it was supposed to do.
    I printed the sort.c document from my desktop and compared it to the sort.c
    document from my laptop line by line character by character. No difference!

    What on earth is going on???

    here is the code from my desktop (the one that compiled correctly):

    /*inputs a list of strings from the keyboard, sorts them,*/
    /*and then displays them on the screen*/
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define MAXLINES 25
    int get_lines(char *lines[]);
    void sort(char *p[], int n);
    void print_strings(char *p[], int n);
    char *lines[MAXLINES];
    int main(void)
    	int number_of_lines;
    	/*read in the lines from the keyboard*/
    	number_of_lines = get_lines(lines);
    	if ( number_of_lines < 0)
    		puts("Memory allocation error");
    	sort(lines, number_of_lines);
    	print_strings(lines, number_of_lines);
    	return (0);
    int get_lines(char *lines[])
    	int n = 0, slen;
    	char buffer[80]; /*temporary storage for each line*/
    	puts("Enter one line at a time; enter a blank line when done.");
    	while ((n < MAXLINES) && (fgets(buffer, 80,stdin) != 0))
    		slen = strlen (buffer);
    		if (slen < 2)
    		buffer [slen-1] = 0;
    		if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
    	strcpy( lines[n++], buffer );
    	return n;
    } /*end of get_lines()*/
    void sort(char *p[], int n)
    	int a, b;
    	char *x;
    	for (a=1; a<n; a++)
    		for (b = 0; b < n-1; b++)
    			if (strcmp(p[b], p[b+1]) > 0)
    				x = p[b];
    				p[b] = p[b+1];
    				p[b+1] = x;
    void print_strings(char *p[], int n)
    	int count;
    	for (count = 0; count < n; count++)
    		printf("%s\n", p[count]);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    And you're 100% positive that the print_strings() function exists in the file on your laptop?
    If you understand what you're doing, you're not learning anything.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Don't recreate it. Copy it over there. That way you know for sure you're using the same file, so you know you're dealing with the same issue.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2005

    Found it!


    In the main() it the on line 29 the print_strings() is called:

    print_strings(lines, number_of_lines);
    Also, the prototype for print_strings() is declared befor main():

    void print_strings(char *p[ ], int n);
    And the the print_strings() definition is after main():
    void print_strings oooops!!!
    I just found the problem and recompiled the program!

    In the function definition I had:
    void print_string(....
    But in the prototype and function call string is plural "print_strings"

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM