Array Char to String with

This is a discussion on Array Char to String with

Thread: Array Char to String with \0

within the C Programming forums, part of the General Programming Boards category; Hi, I need convert, a Char Array to string, and i dont know if it has 0. Code: void charArrToStr() ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    158

    Array Char to String with \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
    452
    you need to allocate memory for teste before you use it.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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
    2,613
    You might wish to use the strncpy function.

    Tim S.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    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,528
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21