0

What does (char* )str do in the below code?

/**
 * Main file 
 */
#include <assert.h>
#include <mylib.h>

int main()
{
  const char str[] = "this is my first lab\n";
  int ret=1; 

  ret = my_print((char *)str, sizeof(str));

  assert(!ret);

  return 0;
}

This code is written by my instructor. my_print is a function which receives a pointer to a string and the size of that string. I am confused on why do we have to use (char *)str to pass the string to the my_print function. What does it actually do?

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Assasins
  • 1,593
  • 5
  • 20
  • 22
  • possible duplicate of [what is array decaying?](http://stackoverflow.com/questions/1461432/what-is-array-decaying) – m0skit0 Mar 08 '13 at 11:45
  • 1
    @m0skit0 Not necessarily, since the purpose of the cast could also be to bypass the const (which is very bad practice). – Lundin Mar 08 '13 at 12:17

3 Answers3

10

It casts away the const.

This means it makes your program likely to crash in case my_print modifies that string since its memory may be marked as read-only. So it's generally a bad idea to remove the const modifier through a cast.

In your case it looks a bit like whoever implemented my_print didn't think that string to be printed would never have to be modified and thus didn't make it accept a const char * argument.

So what you should do instead of the cast is changing the definition of my_print to accept a const char * instead of a char * as its first parameter.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • The task was to implement the my_print function in x86 assembly – Assasins Mar 08 '13 at 11:50
  • Can you elaborate me more on this? I'm kinda confused. :( – Assasins Mar 08 '13 at 12:32
  • This is simple: the cast is removing the `const` modificator from previous declaration. What part you did not understand? – m0skit0 Mar 08 '13 at 12:37
  • what was the whole point of casting it then? If you were to write the above function call, how would you write it? – Assasins Mar 08 '13 at 12:50
  • Okay. I just checked the mylib.h header file and it contains the definition as "int my_print(char *str, int size);". And I am required to write the my_print function in x86 assembly. DO you think there was a reason to define the string as a constant and then cast it when passing as an argument? – Assasins Mar 08 '13 at 13:24
  • 1
    No. The function header will be C anyway, just the body will be in assembly. So you can easily use `const char *str` there. – ThiefMaster Mar 08 '13 at 13:47
  • 1
    The '*promise*' of `const` and an argument qualifier cannot be enforced by the assembler, so arguably declaring it const would have no semantic purpose. The compiler on the other hand will not allow you to pass a const to an non-const arg without an explicit '*shut-up I know what I am doing* type cast' (because the compiler can enforce that promise in C code). The simplest solution would be not to declare the string as const in the first instance - no point in making promises you can't guarantee to keep. – Clifford Mar 08 '13 at 14:01
5

That is "type casting" (or "type conversion"). In other words, it tells the compiler to treat one type as another type.

What this specific conversion does is tell the compiler to treat the constant string as not constant. If the called function tries to modify the string, it may not work, or may even crash the program, as modifying constant data is undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • can you elaborate more on this? Since now the constant char is not a constant, how could the program crash when it tries to modify the string? – Assasins Mar 08 '13 at 12:42
  • @Fazlan Constant data is often stored in a memory section marked as read-only. When attempting to write to this section the CPU will catch that and cause CPU trap propagating up to your program as a crash. – Some programmer dude Mar 08 '13 at 12:44
  • 1
    @Fazlan Now you're going around in circles I think. Lets take the program in your question, for example. What if you called _another_ function that takes a non-constant `char *`, and that function _does_ modify the string. Then calling that function with a constant string like you have will cause problems. – Some programmer dude Mar 08 '13 at 12:58
2

It is a typecast, i.e. it changes the datatype. (char*) means type cast to type "pointer to char"

uba
  • 2,012
  • 18
  • 21