Thread: Array Char to String with backslash-0

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    Array Char to String with backslash-0

    Hi,

    I need convert, a Char Array to string, and i dont know if it has 0.

    Code:
    void charArrToStr()
    {
    	char aux[30];
    	char * teste;
    
    	memset(teste, 0, 30);
    
    	teste[0] = 't';
    	teste[1] = 'e';
    	teste[2] = 's';
    
    	strcpy(aux, teste);
    	printf(aux);
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    you need to allocate memory for teste before you use it.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If you had an array of chars, you could make it a string by adding a '\0' at the end of it.

    Code:
    char * teste;
    That is a pointer to char. Depending on your system, doing sizeof(teste) will get you either 4, or 8 bytes*. There is nowhere near 30 bytes to play with there. I think you are confused about the difference between pointers and arrays. Read through some of these questions and answers - hopefully it will clarify the difference between a pointer and an array for you.

    *: Assuming you are running an x86 or x86_64 machine, of course.
    Last edited by kermit; 07-21-2010 at 04:49 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You might wish to use the strncpy function.

    Tim S.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    He is using that, but on a a pointer to char that has no memory malloc'd for it. So it fails anyway.

    Also, among other things, printf(aux) doesn't work. Go read up on the proper way to call printf.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    What's the cost of calling calloc instead of malloc? You may consider using calloc to get mem that is already zeroed. Just remember to allocate space for the '\0', something like "calloc(strlen(aux) + 1, sizeof(char))"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by KBriggs View Post
    Also, among other things, printf(aux) doesn't work. Go read up on the proper way to call printf.
    Yes it does, provided the string is formatted correctly:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char buf[BUFSIZ] = "hello world\n";
        printf( buf );
        return 0;
    }
    And...
    Quote Originally Posted by User Name: View Post
    What's the cost of calling calloc instead of malloc? You may consider using calloc to get mem that is already zeroed. Just remember to allocate space for the '\0', something like "calloc(strlen(aux) + 1, sizeof(char))"
    sizeof( char ) is always one.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Using sizeof(x) even when you know what it's going to be is like using defines instead of magic numbers, it explains what the "1" would not. It's a personal preference, nothing more. Although it should be noted that using sizeof doesn't detract from performance, it's substituted for it's value at compile time.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by User Name: View Post
    What's the cost of calling calloc instead of malloc? You may consider using calloc to get mem that is already zeroed. Just remember to allocate space for the '\0', something like "calloc(strlen(aux) + 1, sizeof(char))"
    One would think, given the convenience of zero filled memory, that malloc would rarely be used, and calloc would be preferred. Yet this is not generally the case. Have a look at the following from the C-FAQ:




    Quote Originally Posted by User Name: View Post
    Using sizeof(x) even when you know what it's going to be is like using defines instead of magic numbers, it explains what the "1" would not. It's a personal preference, nothing more. Although it should be noted that using sizeof doesn't detract from performance, it's substituted for it's value at compile time.
    It also gives a size_t value to calloc, which is what calloc expects.
    Last edited by kermit; 07-22-2010 at 04:09 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf(aux);
    NEVER pass variable string data to printf as the format string.

    If anyone ever sneaks a % character in there, you're hosed!
    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.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Totally agree with salem for ' printf(aux);'
    Use it if you want format string attack?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bus Error Appending Char to String (char array)
    By seal308 in forum C Programming
    Replies: 8
    Last Post: 02-02-2016, 12:12 PM
  2. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  3. Replies: 1
    Last Post: 02-05-2010, 02:59 PM
  4. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  5. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 06:48 AM