Thread: Assign null to a character array.

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Assign null to a character array.

    Suppose there is a character type array. So, if i like to assign null to it. I have to done as following way (like given code). But I like to assing null to all its index without individually assign null to each index. How does it possible please help. My present technique is like this..............
    Code:
    #include<stdio.h>
    
    int main()
    {
    	char a[100];
    	int i;
    	for(i = 0; i < 100; i++)
    		a[i] = '\0';
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you can do something like this:
    Code:
    char a[1000] = { 0 };
    That will fill the whole array with zeros.

    Or, if you have something that has been used previously, and you want to "clear it", you can use memset().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Character Array: If Null
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:06 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM