Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
3.89/5 (4 votes)
See more:
String is an array of chars right?
So like all(static)arrays it's size has to be set before execution
Where does this happen?
Also,when I crate an array of strings and I increase it's pointer by one to go to the next string how does the system knows the next address?

Also,why can't we have arrays of different data types?Is it because of memory size?
If I create a structure A containing a 64-bit integer and a structure B containing two 32-bit integers why can't I put them in the same array?

Thank you very much !
Posted

Length of string is determined in runtime dynamically. String has length and capacity properties, when you define a new string it allocates extra space to create additional buffer in case you could expand this string.

For instance,

C++
string str("abcdefgh");


length: 8
capacity: 15

If it had capacity of 8, adding any characters to the string would force a reallocation. By making the capacity larger than the actual string, this gives the user some buffer room to expand the string before reallocation needs to be done. Reallocation is expensive because, each character in the string has to be copied to the new memory.

And when you are creating an array of string, you are creating an array of pointers which holds start address of strings.

If you want to create array of different types you need to use array of void pointers like this;

C++
void *ary[10];
ary[0] = new int();
ary[1] = new float();
 
Share this answer
 
Comments
[no name] 22-May-14 0:16am    
Try to avoid "void *".
Emre Ataseven 22-May-14 1:09am    
And use what?
[no name] 22-May-14 2:04am    
Use boost::variant instead as it is the safest way for doing this. Read this page:
http://www.stroustrup.com/bs_faq.html
In the page read the answer for the question "Why does C++ allow unsafe code?".

You can use boost::variant like this:
typedef boost::variant<int, bool=""> vType;
vType a = 10;
vType b = false;

Then you can retrieve the value like this:
bool value = boost::get<bool>(b);
int ivalue = boost::get<int>(a);
Assuming you are asking about C++ std::string.
Quote:
String is an array of chars right?
Very likely, however that is 'implementation dependent'.


Quote:
So like all(static)arrays it's size has to be set before execution
Nope. std::string can grow, hence dynamic memory allocation should happen, sometime.


Quote:
Also,when I crate an array of strings and I increase it's pointer by one to go to the next string how does the system knows the next address?
The next item of the array is adjacent to the current one. An array strictly follow a linear memory arrangement. Its content is a reference to the std::string object.


Quote:
Also,why can't we have arrays of different data types?Is it because of memory size?
Yes, it is (mainly) because of memory size.


Quote:
If I create a structure A containing a 64-bit integer and a structure B containing two 32-bit integers why can't I put them in the same array?
Because the are diffrent types. However, you may create a union containing the two structs and then put the union in an array.
 
Share this answer
 
Yes, a string size has to be determined. But when you do this:

char *example = "Jack";


The size of the string is automatically set by the compiler. "example" is a pointer.

But:

char example[5];
strcpy(example, "Jack");


Here you're determining the size of the string. This string is a static array.
You can accomplish this without using 'strcpy' by the following.

C#
char example[5];
example[0] = 'J';
example[1] = 'a';
example[2] = 'c';
example[3] = 'k';
example[4] = '\0';


The results are same. This is done to ensure you that strings are also arrays.
 
Share this answer
 
In C, the usual way to define a string is an array of char. The other solutions point out some things to watch out for when dealing with those. You can put different types inside the same array if you define an array of union, or an array of pointers that point to the actual data.

In C++, you should use the class std::string which removes most of the issues with char arrays. You can put different types into the same array by defining it as an array of pointers to a common base class: e. g. you can define an array with pointers to Shape objects, and then store pointers to Square or Circle objects in that array, provided that Square and Circle are classes derived from Shape.

When you have an array of C-strings, then what you really have is an array of pointers to char. I. e. each element points to the memory location of the string, and iterating over the array simply moves from one pointer to the next. Since pointer (to specific type) sizes are always the same, the compiler can easily figure that out.

When you have an array of std::string, each std::string instance contains a pointer to the actual string, whereas the static size of the object remains the same. Therefore, again, the compiler can easily figure out how to iterate from one to the next.
 
Share this answer
 

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