Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string array conversion

Tags:

c

ansi-c

I have the following code:

char *array1[3] = 
{
    "hello",
    "world",
    "there."
};

struct locator_t
{
    char **t;
    int len;
} locator[2] =
{
    {
        array1,
        10
    }
};

It compiles OK with "gcc -Wall -ansi -pedantic". But with another toolchain (Rowley), it complains about

warning: initialization from incompatible pointer type

on the line where char **t is. Is this indeed illegal code or is it OK?

Thanks for all the answer. I now know where my problem was. However, it raises a new question:

string array initialisation

like image 389
lang2 Avatar asked Oct 20 '11 10:10

lang2


People also ask

How do I convert a string to an array of numbers?

You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.

Can you turn a string into an array JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.

Which method converts an array to string?

Arrays.toString() method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space).

Can we convert string to array in C?

The c_str() and strcpy() function in C++C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').


2 Answers

Seems perfectly legal to me; char *[3] decays to char **, so the assignment should be valid.

Neither GCC 4.4.5 nor CLang 1.1 complains.

like image 188
Fred Foo Avatar answered Sep 23 '22 03:09

Fred Foo


Although in practice array1 should decay to a pointer of type char **, its real type is in fact char *[3], hence the warning.

To suppress the warning, you could try casting it explicitly:

...
(char **) array1;
...
like image 29
Blagovest Buyukliev Avatar answered Sep 22 '22 03:09

Blagovest Buyukliev