简体   繁体   中英

C - Convert int to char

I am looking for a solution for my problem (newbie here).

I have an array of strings ( char** arrayNum ) and I would like to store the number of elements of that array at the first index.

But I can't find the right way to do convert the number of elements as a string (or character).

I have tried itoa , casting ( char ), +'0' , snprintf ... nothing works.

As every time I ask a question on Stack Overflow, I am sure the solution will be obvious. Thanks in advance for your help.

So I have an array of strings char** arrayNum which I populate, leaving the index 0 empty.

When I try to assign a string to arrayNum[0] :

  • This works: arrayNum[0] = "blabla";

  • This does not work: arrayNum[0] = (char) ((arraySize - 1)+'0');

I have tried countless others combinations, I don't even remember...

arrayNum can be thought of as an array of strings ( char * ). So you will naturally have trouble trying to assign a char (or indeed any type other than char * ) to an element of this array.

I think it would preferable to store the length of the array separately to the array. For example, using a struct . To do otherwise invites confusion.

If you really, really want to store the length in the first element, then you could do something like:

arrayNum[0]    = malloc(sizeof(char));
arrayNum[0][0] = (char) ((arraySize - 1)+'0');

This takes advantage of the fact that arrayNum is strictly an array of pointers and each of those pointers is a pointer to a char . So it can point to a single character or an array of characters (a "string").

Compare this for clarity with (say):

struct elements {
    int length;
    char **data;
};

arrayNum is not an "array of strings."

It might be useful for you to think about it that way, but it is important for you to know what it really is. It is an array of pointers where each pointer is a pointer to char .

Sometimes a pointer to char is a "string," and sometimes it's a pointer into the middle of a string, and sometimes it's just a pointer to some character somewhere. It all depends on how you use it.

The C programming language does not really have strings. It has string literals , but a string literal is just a const array of characters that happens to end with a \\000 . The reason you can write arrayNum[0] = "blabla"; is because the value of the string literal "blabla" is a pointer to the first 'b' in "blabla", and the elements of the arrayNum array are pointers to characters.

It's your responsibility to decide whether arrayNum[i] points to the first character of some string, or whether it just happens to point to some single character; and it's your responsibility to decide and keep track of whether it points to something that needs to be freed() or whether it points to read-only memory, or whether it points to something on the stack, or whether it points to/into some staticly allocated data structure.

The language doesn't care.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM