Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can easily do: char arr[3]={'O','a','b'};
but can i write it as: char arr[3]={"hi","how","are"};
please help me , i am a begginer

What I have tried:

I have tried entering
char arr[3]={'o','a','b'}
but doubt for the other type as mentioned above
Posted
Updated 7-Jul-18 7:29am
Comments
Bryian Tan 6-Jul-18 0:35am    
You can try something like
char *yourArr[3]={"hi","how","are"};

The problem is that you have to declare variables as teh correct type in order to use them:
char arr[3]
Allocates enough space for 3 char items - and in C char is almost certainly ASCII which means it is 8 bits wide, so even on a 64 bit system the arr array occupies only three bytes of memory.
But this:
{"hi","how","are"}
is not three characters, it's three strings, which are much larger.
"hi"   requires 3 bytes: 'h', 'i', and a null terminator.
"how"  requires 4 bytes: 'h', 'o', 'w', and a null terminator.
"are"  requires 4 bytes: 'a', 'r', 'e', and a null terminator.
In addition, you don't store the data in arr itself, arr requires three pointer to char values in order to access the strings, and each pointer needs 8 bytes on a 64 bit system! If it didn't work like that, you couldn't efficiently index through the strings as the "distance" between two strings would not be the same each time.
To use the string initializer, you need to declare arr so that it has enough space to store them, and that means declaring it as an array of pointer to char values:
C++
char* arr[3] = {"hi","how","are"};
 
Share this answer
 
Comments
CPallini 6-Jul-18 4:05am    
5.
Mr. Griff is correct. Also be aware that often the size of the array is not specified. Here's an example :
C++
const char *strings[] =
{
   "first string",
   "second string",
   "third string"
};
and sometimes a terminator is added so you can loop until the null is found:
C++
const char *strings[] =
{
   "first string",
   "second string",
   "third string",
   NULL
};

for( int n = 0; strings[n] != NULL; ++n )
{
   // do something with the string here
}
 
Share this answer
 
v2
Comments
CPallini 6-Jul-18 17:26pm    
AFAIK nullptr is not available in C programming language.
Rick York 6-Jul-18 18:46pm    
Yes, you are right. It has been so long since I used plain C I had forgotten. I will update the post.
CPallini 7-Jul-18 3:23am    
5.
The answers are corrected, but to better understand that very important stuff you should learn from this tutorial.

In the end most coders are using some classes for dealing with texts. They have often useful functions like finding or replace. An introduction into std:string with some chapters.
 
Share this answer
 
Comments
CPallini 7-Jul-18 16:59pm    
Well, C coders don't use classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900