Solved

C, how to define an array of string

Posted on 2012-03-22
4
499 Views
Last Modified: 2012-03-24
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.
0
Comment
Question by:rgbcof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37755544
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
 
LVL 34

Expert Comment

by:sarabande
ID: 37757280
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
 
LVL 3

Accepted Solution

by:
Simplist earned 350 total points
ID: 37757311
// 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
 

Author Closing Comment

by:rgbcof
ID: 37762151
Excellent.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question