Home / Blue J / How to Null a string or empty a string array

How to Null a string or empty a string array

Null a String/ Empty a string array
Null a string in C using pointer string or array string. Many times the string is nullified assigning arr[0]=’\0′ but this will not nullify the array as only the first index will be NULL.

 

1. Null a string using pointer string.
char *ptr="hello";
printf("%s" , ptr);
*ptr='\0';
printf("\n %s  and  %s ",ptr,ptr+1);

 

TO show that array/pointer is not actually empty or null the first array null but the rest of array characters are still on the same address.

ptr=””;                                  
printf(“%s and %s”, ptr,ptr+1);

OUTPUT:

hello
  and ello


2. Null a string using array string

Null a string using an array is not efficient as though the first index is nullified but the rest of the characters still have the same address.

char arr[30]="hello";
arr[0]='\0';
printf("null string=%s and %c",arr,arr[2]);

 


OUTPUT:

null string= and l

 

Check Also

program for display lower triangular matrix in java

What is Lower Triangular Matrix? Lower Triangular Matrix is if all the elements above the main ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.