Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create an Array of Char pointers in C?

I am new to C, and things are different in C than in any other language I've learned. In my homework I want to create an array of chars which point to an array of chars, but rather than make a multidimensional char array, I figure I'd have more control and create char arrays and put each individual one into the indexes of the original char array:

char keywords[10];
keywords[0] = "float";

The above example is to clarify and a simple case. But my question is due to the research I've been doing, and I am confused about something. Normally this would work in other languages, but in C it would be:

char *keyword[10];
keywords[0] = "float";

But when I want to send it through a function, why is this necessary:

void function(char **keyword); //function prototype

Wouldn't just passing the array pointer be enough?

like image 743
Andy Avatar asked Feb 19 '12 19:02

Andy


People also ask

Can you make an array of pointers in C?

Yes, we can. When we require multiple pointers in a C program, we create an array of multiple pointers and use it in the program. We do the same for all the other data types in the C program.

Can you have an array of pointers?

We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values.

What is a char pointer array in C?

An array of pointers to strings is an array of character pointers where each pointer points to the first character of the string or the base address of the string. Let's see how we can declare and initialize an array of pointers to strings.

Can you have an array of chars?

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.


2 Answers

It looks like you're confused by the double stars in

void function(char ** keyword);

The double stars just means that this function expects you to pass a pointer to a pointer to a char. This syntax doesn't include any information about the fact that you are using an array, or that the char is actually the first char of many in a string. It's up to you as the programmer to know what kind of data structure this char ** actually points to.

For example, let's suppose the beginning of your array is stored at address 0x1000. The keyword argument to the function should have a value of 0x1000. If you dereference keyword, you get the first entry in the array, which is a char * that points to the first char in the string "float". If you dereference the char *, you get the char "f".

The (contrived) code for that would look like:

void function(char **keyword)
{
    char * first_string = *keyword;   // *keyword is equivalent to keyword[0]
    char first_char = *first_string;  // *first_string is equivalent to first_string[0]
}

There were two pointers in the example above. By adding an offset to the first pointer before dereferencing it, you can access different strings in the array. By adding an offset to the second pointer before dereferencing it, you can access different chars in the string.

like image 174
David Grayson Avatar answered Sep 24 '22 18:09

David Grayson


char *keyword[10];

keyword is an array 10 of char *. In a value context, it converted to a pointer to a char *.

This conversion is a part of what Chris Torek calls "The Rule":

"As noted elsewhere, C has a very important rule about arrays and pointers. This rule -- The Rule -- says that, in a value context, an object of type ‘array of T’ becomes a value of type ‘pointer to T’, pointing to the first element of that array"

See here for more information: http://web.torek.net/torek/c/pa.html

The C-FAQ also has an entry on this array to pointer conversion:

Question 6.3: So what is meant by the "equivalence of pointers and arrays'' in C?

http://c-faq.com/aryptr/aryptrequiv.html

like image 22
ouah Avatar answered Sep 22 '22 18:09

ouah