Andrew Cruey Andrew Cruey - 23 days ago 9
C Question

Reassigning String Value in C

So I'm a first year C student and I'm working on a project where we must create a times table in a given base that the user inputs. I've almost completely finished it but I'm having trouble removing leading zeros from the table.

This is what the simple base 10 table looks like.

andrew@andrew-VirtualBox ~/cs211/project1 $ ./timesTable 10

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
1 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 |
2 | 00 | 02 | 04 | 06 | 08 | 10 | 12 | 14 | 16 | 18 |
3 | 00 | 03 | 06 | 09 | 12 | 15 | 18 | 21 | 24 | 27 |
4 | 00 | 04 | 08 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |
5 | 00 | 05 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |
6 | 00 | 06 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |
7 | 00 | 07 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |
8 | 00 | 08 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |
9 | 00 | 09 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |
---|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|


I want to remove the leading zeros from each number but I'm struggling to get it working. Here's what the function code looks like.

char * toHex(int n, int q){

static char hexNumber[4] = {0x00};
char digits[16]={ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

unsigned int un = q;


int i;

for(i=1; i>=0; i--){
hexNumber[i] = digits[un%n];
un=un/n;
}
if(strcmp(hexNumber, "0") == 0){
hexNumber = " ";
}
return hexNumber;
}


Using my logic, I'm trying to reassign the hexNumber string to an empty " " string so the zeros just don't appear, but I get the error:

timesTable.c: In function ‘toHex’:
timesTable.c:68:13: error: incompatible types when assigning to type ‘char[4]’ from type ‘char *’
hexNumber = " ";


Any help would be greatly appreciated.

Answer

You can't reassign char[] types, and you can't modify a const char[]. So you must do:

  • char hexNumber[4] = {0x00};

And when you want to assign:

  • memcpy(hexNumber, " ", 2) (if you have strings.h imported)
  • hexNumber[0] = ' '; hexNumber[1] = '\0'; (if you don't want to import anything)

Edit: The above fixes the error. For correctness of your program, maybe this is what you want to replace your if(strcmp... code with?

for (i = 0; i < 3; i++) {
    if (hexNumber[i] == '0') {
        hexNumber[i] = ' ';
    } else {
        break;
    }
}

This will check from the start of the string for 0 characters, replacing them with space characters until it reaches a non-0 character.

You also can't return hexNumber if you define it as an array on your stack, so instead do char* hexNumber = malloc(4); then memset(hexNumber, '\0', 4) (make sure you import <strings.h> at the top). This allocates it on the heap instead of the stack. Whatever calls this function will need to eventually free the result or else you'll have a memory leak. Alternatively, you could make this function accept hexNumber as an input and make the caller allocate it however it wants, which is what many C library functions dealing with strings do.