Making a post to show what I learned about c strings
Give feedback on how wrong I am. Not too experienced. Thanks everyone who has helped me.
arrays decompose into pointers, so char *ha sorta equals char ha[]
char *stuff[] is an array of pointers to char array. pointers are 4 bytes on a 32 bit architecture and 3 bytes on ce. That adds an extra 4/3 bytes to the string, which is larger than an array.
To store string array:
use an array with pointers that point to many char arrays. This is not good for short strings, as the pointer takes up 4/3 characters of space.
use 2d array with the second array beginning the largest size of the array. This is not very good as it will result in many bytes not used. This is good if the strings have similar lengths. I believe that it also has space for pointers, which is another use of space
Another method is to use an char arr[] with nulls separating the strings and another list for the index of the beginning of the string. Use a while loop to get a string from the long array. I've written a java program to generate both arrays, given a string array.
Another way is to use an array that stores both the beginning and end, without the nulls. For small arrays with index < 256 this would be the same as the top. The difference is that you can use for loop. For size greater than a char, this would take more space.
Consider these methods only if you want to hyper optimize the code
I wrote a program in my free time in java to see which method saves the most space.
Tell me if you want me to post source of these java programs. I'll probably do it later on request, I don't want to start up eclipse right now.
Give feedback on how wrong I am. Not too experienced. Thanks everyone who has helped me.
arrays decompose into pointers, so char *ha sorta equals char ha[]
char *stuff[] is an array of pointers to char array. pointers are 4 bytes on a 32 bit architecture and 3 bytes on ce. That adds an extra 4/3 bytes to the string, which is larger than an array.
To store string array:
use an array with pointers that point to many char arrays. This is not good for short strings, as the pointer takes up 4/3 characters of space.
use 2d array with the second array beginning the largest size of the array. This is not very good as it will result in many bytes not used. This is good if the strings have similar lengths. I believe that it also has space for pointers, which is another use of space
Another method is to use an char arr[] with nulls separating the strings and another list for the index of the beginning of the string. Use a while loop to get a string from the long array. I've written a java program to generate both arrays, given a string array.
Another way is to use an array that stores both the beginning and end, without the nulls. For small arrays with index < 256 this would be the same as the top. The difference is that you can use for loop. For size greater than a char, this would take more space.
Consider these methods only if you want to hyper optimize the code
I wrote a program in my free time in java to see which method saves the most space.
Tell me if you want me to post source of these java programs. I'll probably do it later on request, I don't want to start up eclipse right now.