Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of char pointers

I am looking at some code I did not write and wanted help to understand an element of it. The code stores character arrays, creates pointers to these arrays (assigning the pointers the arrays addresses). It looks like it then creates an array to store these characters pointers addresses and I just wanted some clarification on what I am looking at exactly. I also am confused about the use of the double (**) when creating the array.

I have included a stripped down and simplified example of what I am looking at below.

char eLangAr[20] = "English";
char fLangAr[20] = "French";
char gLangAr[20] = "German";

char* eLangPtr = eLangAr;
char* fLangPtr = fLangAr;
char* gLangPtr = gLangAr;


char **langStrings [3]=
{
    &eLangPtr,
    &fLangPtr,
    &gLangPtr
};

When using the array they pass it as an argument to a function.

menu (*langStrings[0]);

So the idea is that the character array value "English" is passed to the function but I'm having trouble seeing how. They pass the menu function a copy of the value stored in the langStrings function at location 0, which would be the address of eLandPtr? If someone could explain the process in English so I could get my head around it that would be great. It might be just because it has been a long day but its just not straight in my head at all.

like image 923
user1649972 Avatar asked Feb 12 '18 15:02

user1649972


People also ask

What is array of pointers with example?

In computer programming, an array of pointers is an indexed set of variables, where the variables are pointers (referencing a location in memory). Pointers are an important tool in computer science for creating, using, and destroying all types of data structures.

What is a char * array in C?

In C, an array of type char is used to represent a character string, the end of which is marked by a byte set to 0 (also known as a NUL character)

How do you allocate memory to the array of char pointers?

In C, the RAM should use the following code to allocate the two-dimensional array: *contents = (char**) malloc(sizeof(char*) * numRows); **contents = (char*) malloc(sizeof(char) * numColumns * numRows); for(i = 0; i < numRows; i++) (*contents)[i] = ( (**contents) + (i * numColumns) );

What is the difference between char array and char pointer?

For the array, the total string is stored in the stack section, but for the pointer, the pointer variable is stored into stack section, and content is stored at code section. And the most important difference is that, we cannot edit the pointer type string.


1 Answers

You are correct that langStrings contains pointers to pointers of array of characters. So each langString[i] points to a pointer. That pointer points to an array. That array contains the name of a language.

As others point out, it looks a bit clumsy. I'll elaborate:

char eLangAr[20] = "English"; is an array of 20 chars and the name "English" is copied to it. I don't expect that variable eLangAr will ever contain something else than this language name, so there is no need to use an array; a constant would be sufficient.

char **langStrings [3]= ... Here, it would be sufficient to have just one indirection (one *) as there seems no need to ever have the pointer point to anything else (randomly shuffle languages?).

in conclusion, just having the following should be sufficient:

const char *langStrings [3]=
{
    "English",
    "French",
    "German"
};

(Note the const as the strings are now read-only constants/literals.)


What the given code could be useful for is when these language names must be spelled differently in different languages. So "English", "French", "German" become "Engels", "Frans", "Duits". However, then there still is one level of indirection too many and the following would be sufficient:
char *langStrings [3]=
{
    aLangArr,
    fLangAr,
    gLangAr
};
like image 134
Paul Ogilvie Avatar answered Oct 24 '22 11:10

Paul Ogilvie