4

What I am really trying to achieve is an array of dynamic byte patterns that I can use as a pattern searcher when I buffer binary files. But I am starting off basic for now. I have the following code that I based off of an example found on StackOverflow.

How to Initialize a Multidimensional Char Array in C?

typedef unsigned char BYTE;

int main()
{
    BYTE *p[2][4] = {
        {0x44,0x58,0x54,0x31},
        {0x44,0x58,0x54,0x00}
    };

    return 0;
}

I compile it with mingw32 for Windows.

D:\> gcc example.c -o example.exe

I get the following warnings when I try to compile.

example.c: In function 'main':
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][0]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][1]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][2]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][3]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][0]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][1]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][2]') [enabled by default]

I don't understand the nature of this warning. How do I go about resolving it? Thanks.

1 Answer 1

6

Drop the * from BYTE *p[2][4]:

BYTE p[2][4] = {
    {0x44,0x58,0x54,0x31},
    {0x44,0x58,0x54,0x00}
};

You want a multidimensional array of char: BYTE p[2][4] not a multidimensional array of pointer-to-char.

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

3 Comments

Wow. That was such an easy fix. I'm curious. Why do you drop the pointer when the example I linked used a pointer for something that looks very similar to what I am looking for?
@redsolar In that code the inner-most element is itself an array ("00").
Oh. I see now. Thank you for clearing that up and thank you for your answer. Much appreciated.

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.