filling a char array

This is a discussion on filling a char array within the C Programming forums, part of the General Programming Boards category; Hello, I am passing a char array. And clearing it and setting it with new data. However, in each case ...

  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
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    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, 07:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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