What is the difference between the following two declarations?
char array[] = “Hello World”;
char *array = “Hello World”;
The answer to this question is as follows:
The basic difference between the char array[] and char* array is one is pointer variable and other one is the array that it holds the values
The char *array is a variable that we can initialize with the values but the char array[] is of array type we can perform indexing on this and the value starts from the address that holds the 'H'
and also one major difference is running time complexity
For example:
char *array="world" if you want to print from "orld" then you can shift the pointer to the right by using array++
it takes O(1) time complexity
but with the char array[] takes O(n) time complexity because it needs to run through out the loop so it takes for loop and taking the index values from the 1 to n so it takes the time complexity of O(n)
Get Answers For Free
Most questions answered within 1 hours.