1

I have this code

char *c="hello";
char *x=malloc(sizeof(char)*5+1);
memcpy(x,(char[2])c,sizeof("hello")+1);
x[5]='\0';
printf("%s\n",x);

it gives error that cast specifies array type. why is that? what's the reason behind this error. Does this mean I can't cast like with array type meaning:I can cast to pointer like (char *) but I can't cast to array type like (char[2]) or (char[]) Am I missing anything?

What I need to do in my code to resolve the error cast specified array type error at

 10 |         memcpy(x,(char[2])c,sizeof("hello")+1);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    `x` is a pointer to an array of six characters, with indexes from `0` to `5`. `x[6]` is out of bounds. – Some programmer dude Nov 17 '21 at 12:17
  • 1
    As for your problem, what is the purpose of the cast in `(char[2])c`? What are you trying to accomplish with that? What is the problem it's supposed to solve? What is your expected result of the `memcpy` call? – Some programmer dude Nov 17 '21 at 12:19
  • 1
    Even if you fix the cast and treat `c` like an array of two characters, it will just decay back to a pointer, and the cast won't magically have inserted a null terminator. Try saying exactly what you want to achieve, rather than focus on fixing the compile error on a line that's never going to do what you want anyway. – Useless Nov 17 '21 at 12:19
  • @Someprogrammerdude `As for your problem, what is the purpose of the cast in (char[2])c?` I just like to see casting is working or not? why its not working? – user786 Nov 17 '21 at 12:21
  • @Someprogrammerdude I have corrected it – user786 Nov 17 '21 at 12:22
  • @Useless `cast won't magically have inserted a null terminator.` yes I know I just want to see cast works or not – user786 Nov 17 '21 at 12:23
  • The cast doesn't work, that's what the compiler is telling you. But why are you doing that cast? – Some programmer dude Nov 17 '21 at 12:24
  • `sizeof("hello")` is valid? I would have done `strlen("hello")`. – 500 - Internal Server Error Nov 17 '21 at 12:25
  • @Someprogrammerdude I like to extract first two letters from c in one line – user786 Nov 17 '21 at 12:25
  • `strncpy(x, c, 2); x[2] = '\0';`? – Some programmer dude Nov 17 '21 at 12:25
  • So the correct way to do that is by passing `2` as the size parameter (instead of `sizeof("hello")+1` which you *know* is not the size you want) – Useless Nov 17 '21 at 12:26
  • @Someprogrammerdude so u are saying casting to array type is wrong – user786 Nov 17 '21 at 12:26
  • 2
    @500-InternalServerError Literals strings like `"hello"` are really arrays of characters (including null-terminator). So `sizeof` works and returns the size of the array itself (which will include the null-terminator, unlike `strlen`). This also means that `sizeof("hello") + 1` is too much to copy and will copy one extra character out of bounds. – Some programmer dude Nov 17 '21 at 12:27
  • 1
    Yes. The compiler is telling you it's wrong, and I've told you it wouldn't do what you want anyway. You can look at the definition of [`memcpy`](https://en.cppreference.com/w/c/string/byte/memcpy) and see that its second parameter is a pointer. The size information is passed explicitly as the third parameter, so just put the correct value there. – Useless Nov 17 '21 at 12:30
  • thanks all understood – user786 Nov 17 '21 at 12:31
  • 1
    you cannot cast a pointer to an array. But you **can** cast it to a pointer to array. Try `(char*[2])c` – tstanisl Nov 17 '21 at 12:33
  • @tstanisl `(char*[2]) c` is c is pointer to array of two char? Can u please tell me this – user786 Nov 17 '21 at 13:17
  • @user786, sorry, it should be `(char(*)[2])c` – tstanisl Nov 17 '21 at 14:14

1 Answers1

1

Arrays do not have the assignment operator. You may not write for example

char *c="hello";

char s[6];

s = c;

As a result such a casting like

( char[6] )c

is not allowed.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335