Thread: filling a char array

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    filling a char array

    Hello,

    I am passing a char array. And clearing it and setting it with new data. However, in each case it only display the first 4 chars.

    my output
    main
    load
    fill
    fill

    I am tried using a combination of memmove and strncpy. But still couldn't get it to work. I thought it you pass it by reference you should be able to change the actual value. Which it does, but only the first 4 characters.

    Many thanks for any advice

    Code:
    #include <string.h>
    #include <stdio.h>
    
    void load_buffer(char *buffer);
    void fill_buffer(char *buffer);
    
    
    int main()
    {
    	char buffer[100];
    	strncpy(buffer, "main", strlen(buffer));
    	printf("%s\n", buffer);
    
    	load_buffer(buffer);
    	
    	printf("%s\n", buffer);
    
    	getchar();
    
    	return 0;
    }
    void load_buffer(char *buffer)
    {
    	memset(buffer, 0, sizeof(buffer));
    	strncpy(buffer, "Load_buffer", sizeof(buffer));
    	printf("sizeof buffer: %d %s\n", sizeof(buffer), buffer);
    
    
    	fill_buffer(buffer);
    }
    void fill_buffer(char *buffer)
    {
    	memset(buffer, 0, sizeof(buffer));
    	memmove(buffer, "fill_buffer", sizeof(buffer));
    	printf("%s\n", buffer);
    }

  2. #2
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    The prototipe for strncpy is this:
    Code:
    char *strncpy(char *dest, const char *src, size_t n);
    where n are the bytes of the src to be copied not the sizeof(dest).
    I have stopped reading Stephen King novels. Now I just read C code instead.

  3. #3
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    you should do this instead
    Code:
    void load_buffer (char *buffer, int size)
    {
      memset(buffer, 0, size);
      strncpy(buffer, "Load buffer", strlen("Load buffer"));
      /* the rest of the code */
    }
    
    /* do the same for fill_buffer() */
    you can not know the size of the buffer unless the buffer is already filled with data and a trailing '\0'
    Last edited by cph; 03-02-2009 at 02:43 AM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Quote Originally Posted by steve1_rm View Post

    Code:
    
    void load_buffer(char *buffer)
    {
    	memset(buffer, 0, sizeof(buffer));
    	strncpy(buffer, "Load_buffer", sizeof(buffer));
    	printf("sizeof buffer: %d %s\n", sizeof(buffer), buffer);
    
    
    	fill_buffer(buffer);
    }
    strncpy(buffer, "Load_buffer", sizeof(buffer));

    Here sizeof statement is wrong. You need to give size of "Load_buffer" not of buffer pointer. as you might be running on 32 bit system, sizeof(buffer) return 4, and not 100..

    same is with memset etc....

    NOTE: you also need to be sure that sizeof ("Load_buffer") doesn't exceed the sizeof your buffer. Though here your buffer is big enough (100 characters) but still do put a check on it.

    ---------
    Seniors please correct me, if m wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM