0
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NUM 8

int main() {
    int i, len, sum, offset, remain;
    char bin[32];
    char hexlist[6][1] = {"A", "B", "C", "D", "E", "F"};
    char hex[NUM] = "00000000";
    int hexlen = NUM;
    while (1) {
        scanf("%s", bin);
        if (strcmp(bin, "0") == 0) {
            break;
        }
        len = strlen(bin);
        offset = 0;
        while (offset < len) {
            sum = 0;

            if (len - offset >= 4) {
                for (i = 0; i < 4; i++) {
                    sum += (bin[len-1-i-offset] - '0') * pow(2, i);
                }
            }
            else {
                remain = len - offset;
                for (i = 0; i < remain; i++) {
                    sum += (bin[len-1-i-offset] - '0') * pow(2, i);
                }
            }

            if (sum > 10)
                // I got "warning: assignment makes integer from pointer without a cast"
                hex[--hexlen] = hexlist[sum%10];
            else
                hex[--hexlen] = (char)(((int)'0')+sum);

            offset += 4;
        }
        printf("%s\n", hex);
    }
    return 0;
}

I tried hex[--hexlen] = (char)hexlist[sum%10];, but I got "warning: cast from pointer to integer of different size"

2 Answers 2

4

What you want is this:

char hexlist[] = {'A', 'B', 'C', 'D', 'E', 'F'};

In C a character constant between double quotes indicates a string of characters, and is ended with a null character \0. A character constant between single quotes indicates a single character.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, or char hexlist[] = "ABCDEF";.
Fred Larson: You are right, and normally I would agree, but in this case an array of character constants seems more appropriate then a string.
@nightcracker, @pmg: I know. But for brevity's sake, I'd be inclined to waste the byte. Doesn't make much difference to me.
Thank you all so much. I've just learned C a week ago, I don't understand pointer clearly. Now my code works well (after I corrected the other errors). I don't know is it appropriate, but I want to know how I add every char to an array dynamically? Not just a predefined char array char hex[NUM] = "00000000".
@Vayn: it's better to post a new question rather than ask one in a comment.
|
1

hexlist is an array of arrays

char hexlist[6][1];

each element of hexlist is an array ... and usually references to such arrays decay to pointers. That's what is hapenning in your code: hexlist[sum % 10] is an object of type char[1] and decays to a pointer.

You then try to assign that pointer to an element of hex, of type char. The types are incompatible and after the default promotions, the compiler complains.

2 Comments

No. In many uses, arrays decay to a pointer to their first element. Read section 6 of the comp.lang.c FAQ ... and, once you're there, all the other sections too :)
The FAQ is nice! Thank you :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.