Thread: check my c code.

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    check my c code.

    I am trying to format a code to print hello fred, so far it only prints hello. the code cannot be replaced with a simple printf statement. Can anybody help me figure out how to print fred too. the code is
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //
    // A short program to print the string "Hello, Fred"
    //
    
    int main (int argc, const char *argv[])
    {
    	char name[5];
    	char ch = name[5];
    
    	char *last_char = &name[5];
    	ch = *last_char;
    
    	char *src = "Fred";
    	char *dst;
    	dst = name;
    
    	// copy the name
    	while (*src)
    	{	
                    src != 0;
                    ++src;
    		*dst = *src;
    	}
    
    
    	char *msg = (char *) malloc (5);
    	strcpy (msg, "Hello, ");
    	strcat (msg, name);
    	puts(msg);
    
    	//free (last_char);
    	free (msg);
    	*last_char = 'A';
    
           //free (msg);
    	msg = (char *) malloc (5);
    	msg = src;
    
    	puts (msg);
    	return 0;
    }
    Last edited by rfm; 09-23-2009 at 04:10 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I am mystified, as I see no way for your while loop to end -- src never changes inside the loop so once you get in you can never get out.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    oh i am sorry
    it should be
    src != 0;
    ++src;
    *dst = *src;

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would certainly need to increment after updating, otherwise you risk greeting "red" instead of "Fred".

    Also that declaration of the variable ch is invalid -- name[5] doesn't exist.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char name[5];
    char ch = name[5];
    
    char *last_char = &name[5];
    ch = *last_char;
    
    ...
    
    *last_char = 'A';
    name[5] is an area of memory that you shouldn't access. The valid indexes for the name array are 0 through 4. The last character of the array is name[4].

    Code:
    char *msg = (char *) malloc (5);
    You should avoid casting the return value of malloc in C.

    Code:
    char *msg = (char *) malloc (5);
    strcpy (msg, "Hello, ");
    strcat (msg, name);
    The string "Hello, " requires a buffer of 8 characters (7 + 1 for the terminating null) to store properly. Concatenating "Fred" requires an additional 4 spaces so your buffer must be at least 12 characters but you've only allocated 5.

    Code:
    msg = (char *) malloc (5);
    msg = src;
    This is a memory leak, you've allocated space but then repoint the pointer elsewhere immediately after the allocation. You've now lost the pointer to that memory location and have no way to free it.
    "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. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 02:22 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  5. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM