Thread: read character pointer to character pointer, but printing garbage

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    read character pointer to character pointer, but printing garbage

    Code:
    char *str2;
    char *first1 = "Learn";
    char *second1 = "C";
    char *third1 = "Online";
    str2=(char *)malloc(strlen(first1)+strlen(second1)+strlen(third1)+3);
    sprintf(str2, "%s %s %s",first,second,third);
    
    
    puts(str2);
    Its printing garbage can you please help me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post something which compiles!
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:12:26: error: ‘first’ undeclared (first use in this function)
    bar.c:12:26: note: each undeclared identifier is reported only once for each function it appears in
    bar.c:12:32: error: ‘second’ undeclared (first use in this function)
    bar.c:12:39: error: ‘third’ undeclared (first use in this function)
    $ 
    $ 
    $ gcc -Wall bar.c
    $ ./a.out 
    Learn C Online
    $ valgrind ./a.out
    ==6252== Memcheck, a memory error detector
    ==6252== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==6252== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==6252== Command: ./a.out
    ==6252== 
    Learn C Online
    ==6252== 
    ==6252== HEAP SUMMARY:
    ==6252==     in use at exit: 15 bytes in 1 blocks
    ==6252==   total heap usage: 1 allocs, 0 frees, 15 bytes allocated
    ==6252== 
    ==6252== LEAK SUMMARY:
    ==6252==    definitely lost: 15 bytes in 1 blocks
    ==6252==    indirectly lost: 0 bytes in 0 blocks
    ==6252==      possibly lost: 0 bytes in 0 blocks
    ==6252==    still reachable: 0 bytes in 0 blocks
    ==6252==         suppressed: 0 bytes in 0 blocks
    ==6252== Rerun with --leak-check=full to see details of leaked memory
    ==6252== 
    ==6252== For counts of detected and suppressed errors, rerun with: -v
    ==6252== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
    When typos are fixed, there is nothing wrong with the code - save for the memory leak at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from an opened file character by character
    By SneakySnake in forum C Programming
    Replies: 1
    Last Post: 12-07-2012, 01:05 PM
  2. Character pointer arrays
    By axe in forum C Programming
    Replies: 5
    Last Post: 11-15-2007, 04:25 AM
  3. Character array pointer
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 11:28 PM
  4. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM
  5. character pointer question
    By osmosis in forum C Programming
    Replies: 3
    Last Post: 07-11-2003, 07:46 AM