• C

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.
rgbcofAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
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.
0
sarabandeCommented:
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
0
SimplistCommented:
// C,
// how to define an array of string, where each string can have a maximum of 50 characters
#typedef char str50[51];
str50[] myArray;

// How would you define a pointer to that string, call it "head",
str50[] *head;

// and how do you set a string to where the head pointer is pointing.
*head[0] = "hello";
*head[1] = "my";
*head[2] = "friend";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgbcofAuthor Commented:
Excellent.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.