Link to home
Start Free TrialLog in
Avatar of rgbcof
rgbcof

asked on

C, how to define an array of string

C, how to define an array of string, where each string can have a maximum of 50 characters.
How would you define a pointer to that string, call it "head", and how do you set a string to where the head pointer is pointing.
Avatar of farzanj
farzanj
Flag of Canada image

One way to do it is like
#include<stdio.h>

typedef char string[50];

int main()
{
    string str_array[] = {"First", "Second", "Third", "1234567890123456789012345678901234567890123456789"};
    printf("First element is %s\n", str_array[0]);
    printf("Max element is %s\n", str_array[3]);
    string * head = str_array;
    printf("Head value is %s\n", *head);
    return 0;
}

Open in new window


Of course the last element is \0 so if you really need 50 elements before \0, you will have to declare the size 51.
as it seems to be an assignment, i will not post code but only explain.

in c a string is an array of char. type char is used for taking characters but it is also a 8-bit signed integer which can take integer values from -128 to +127 decimal. the letter 'A' has decimal value 65, the digit '0' has 48 and a space ' ' is decimal 32. a string of [50] would take up to 50 characters but c needs a zero terminator what means a char with integer value 0. if you write it as a quoted character 0 is equivalent to '\0' where \ is escape character which doesn't count but only is used such that you can differ between digit '0' and zero char '\0'. if you consider the zero terminator, a string[50] only could take 49 characters maximum. instead of a char array you also could use a char pointer pointing to the first character of a char array. as we normally have  a zero terminator you see that a char array and a pointer to char is not so much different. in both cases we have a start address in memory and to find out the length we go on char by char until we reach the zero char. because of that equivalence each array was passed a pointer when you use it as argument to a function, a pointer to its first character.

an array of strings is either a 2-dimensional char array or an array of char pointers. note, that there is a difference between those. you can imagine a 2d-char array as a table (matrix) of char cells where each row is a string. such a matrix - say 20 x 50 - is a contiguous piece of memory which is 1000 characters for the example. a char pointer array of 20 also could have 20 pointers pointing to rows of a 20 x 50 table but it also could have 20 pointers which point to arbitrary memory or all 20 pointers were pointing to the same memory. so actually for the second we could have 21 arrays, one array contains the pointers and each pointer may point to a char array. here we have another difference: a pointer also could be 0 (often written as NULL) what means it is pointing to nowhere. a 2d-array of char never could be 0. all cells could be empty (means 0) but size of array (you could use sizeof function) is always number of rows times number of columns.

Sara
ASKER CERTIFIED SOLUTION
Avatar of Simplist
Simplist
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgbcof
rgbcof

ASKER

Excellent.  Thanks.