Tuesday, July 28, 2009

How do we empty an array using C language? like i want to set the value referenced by an array to NULL.?

More efficient method : memset(string,0x0,sizeof(string));

How do we empty an array using C language? like i want to set the value referenced by an array to NULL.?
actually, a C string is terminated by a zero-byte. To create an empty string, it's enough to set the very first element to zero:





char name[25];





// store a string


strcpy(name, "chris", strlen("chris"));





// delete it - set the first byte to 0


name[0]='\0';
Reply:Easy. Lets create an array of int, and initialize them to 0:





int MyArray[25];





for (int i = 0; i %26lt; 25; i++)


MyArray[i]=0;
Reply:i will go with suvidha, and for clearness:


u know the 1st reply to your question is only assigning the value 0 to the array items, it is always keeping space in the memory, value 0 is treated like any other value


the 2nd reply to your question is the best, it is assiging them to null, means they are refering to nothing in the memory, this is how u empty an array
Reply:Create an integer array





int arr[10],x;


for(x=0;x%26lt;9;x++)


{


arr[x]=null;


}


No comments:

Post a Comment