Home > OS >  How to make the first element of a void* equal to 1 when it's casted as a char*?
How to make the first element of a void* equal to 1 when it's casted as a char*?

Time:01-27

I'm new to C, so I'm still trying to get used to some of the concepts, so apologies if this sounds like a dumb question.

I'm given a void pointer, and I have to check that if the first element of it is equal to 1 when casting it as a char pointer. I basically have to solve the following:

void* ptr;
//Ensure that ((char*)ptr)[0] == 1

So far, what I have is:

void* ptr = malloc(4);
char* temp = "f";
ptr = temp;

I'm still trying to get used to comparing chars and ints in C. I recently learned that it compares their ASCII values. Wouldn't that mean that the since the first number in the ASCII value for "f" is 1 (102), it would mean that ((char*)ptr)[0] == 1? This is probably wrong, but I'm not sure how to fix it. I also know that the C char with an ASCII value of exactly 1 is ^A. I'm not sure how to enter that in as a char* since it just interprets the whole thing as two chars (the ^ and the A).

How am I supposed to set ((char*)ptr)[0] equal to 1? I'm pretty lost on this right now.

CodePudding user response:

This code is not doing what you think it does. THe compiler must be screaming at you about it.

void* ptr = malloc(4);
char* temp = "f";
ptr = temp;

I think you are trying to allocate a 4 byte buffer and then set the first byte of that buffer to "f"

void* ptr = malloc(4);
*((char*)ptr) = 'f';

The first line allocates 4 bytes and puts the pointer to it in ptr

Second line says , treat ptr as a char ptr, put 'f' where that char pointer points.

Easier to read is

// make a copy of ptr saying its a char ptr
char *temp = (char*)ptr;
// store f where it points
*temp = 'f'

Note 'f' not "f". These are 2 different things

  • 'f' is the single char = hex 0x66 or decimal 102 (https://www.asciitable.com/)
  • "f" is a character string composed of 2 bytes 'f' and '\0'

CodePudding user response:

You can specify character codes in a string using octal or hexadecimal escape sequences. So the following will create a string whose first byte is 1 and will satisfy the test in your code.

char *f = "\1"; // or "\x1"

CodePudding user response:

You're right that the proper way to check if the first character is a 1 is ((char*)ptr)[0] == 1.

I recently learned that it compares their ASCII values.

In C, character values are just integers. For example, the character literal 'f' is 102 (assuming you're using ASCII). That means the following is perfectly valid.

int x = 'f';

if ( x > 100 ) {
    printf("Greater than 100\n");
}

It's not the case that the first character is 1, the second 0, and the third 2. 'f' and 102 are the same thing.

On a side note (but a very important one), you've got a memory leak in your code, as @WhozCraig pointed out in the comments. You're assigning ptr to malloc(4) but then reassigning ptr to temp. Therefore, the four bytes allocated by the malloc call are lost.

You can just do

void *ptr;
char *temp = "f";
ptr = temp;
  •  Tags:  
  • Related