0

I am trying to add an int to a Multi-dimensional char Array. After reading the link below I would think I can use sprintf. If I can't use sprintf what is another way I can do this?

http://www.cplusplus.com/reference/cstdio/sprintf/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
    //{"TYPE", "ID", "SCOPE", "VALUE"}
    char *symbol_table_variables[503][4];
    int scope = 0;
    int lower_bound_of_big_boy_counter = 0;
    sprintf (symbol_table_variables[lower_bound_of_big_boy_counter][2], "%d", scope);
    printf("symbol_table_variables[lower_bound_of_big_boy_counter][2] %s \n", 
    symbol_table_variables[lower_bound_of_big_boy_counter][2]);
    return 0;
}
2
Contributors
1
Reply
4
Views
11 Months
Discussion Span
Last Post by AssertNull
0

I am trying to add an int to a Multi-dimensional char Array.

You don't have a multidimensional char array. You have a multidimensional array of char* which you don't want here. Take the asterisk out of line 10 and you'll have a multidimensional character buffer big enough to hold 503 strings provided none of them are more than three characters long (you always have to have space for the NULL terminator that signifies the end of a C String, so since you have a 4 in your second bracket in line 10, subtract 1 and you get 3, the maximum length of each string).

I'm ASSUMING that is what you want since you are using sprintf, but your comment on line 9 would seem to suggest otherwise? The code does as well since you are storing "scope" with an offset of 2, which is in line with the comment on line 9. However, this comment suggests that you do NOT intend to store anything as a string, particularly since, as I mentioned, you leave no room for that NULL terminator that each string must have.

If you are not storing strings, don't use sprintf.

Are you tight on RAM? You can only spare one byte for an integer and that's why you are using a 2-dimensional character array rather than an array of integers or creating a structure and creating a one-dimensional array of that structure? If you are not tight on RAM, I can't see any advantage to storing everything as characters. if you ARE tight on RAM and your integers are all in the range of -128 to 127, I guess you can save space and make everything a character.

Seems to me that creating a structure is the way to go unless there is a reason not to, but I'm not 100% clear what you're trying to do.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct
{
    int type;
    int id;
    int scope;
    int value;
} symbol;



int main(int argc, char *argv[])
{
    //{"TYPE", "ID", "SCOPE", "VALUE"}
    symbol symbol_table_variables[503];
    int lower_bound_of_big_boy_counter = 0;
    symbol_table_variables[lower_bound_of_big_boy_counter].scope = 0;
    printf("symbol_table_variables[lower_bound_of_big_boy_counter].scope %d \n",
        symbol_table_variables[lower_bound_of_big_boy_counter].scope);
    return 0;
}

Edited by AssertNull

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.