2

This example is made up for demonstration, but I need to cast the pointer as in the example

I am getting the following errors:

test2.c: In function ‘main’:
test2.c:25:12: error: expected identifier before ‘(’ token
test2.c:25:12: error: too few arguments to function ‘strcpy’
test2.c:26:20: error: expected identifier before ‘(’ token

The code is this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct test {
        void *ptr;
        char str[300];
};
struct test2 {
        int i;
        char astr[200];
};

int main(void)
{
        struct test *p;
        p = malloc(sizeof(struct test));
        p->ptr = malloc(sizeof(struct test2));
        /*
        void *p2;
        p2 = p->ptr;
        strcpy(((struct test2 *)p2)->astr, "hello world");
        printf("%s\n", ((struct test2 *)p2)->astr);
        */
        strcpy(p->(struct test2 *)ptr->astr, "hello world");
        printf("%s\n", p->(struct test2 *)ptr->astr);
        return 0;
}

The commented-out part of the code works well. I understand that the processor can not dereference the pointer without additional variable and the compiler will create an additional variable, but I want to understand how to cast the pointer that is nested in the structure without creating an additional variable?

Just to make the code look more compact, I will often use a similar thing and I'd like to write it in one line without additional variables.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15
  • 2
    `strcpy(((struct test2*)p->ptr)->astr, "hello world");` – BLUEPIXY Dec 01 '14 at 18:31
  • i need cast like this: `p->(struct test2 *)ptr->astr` but compiler get errors – Ivan Ivanovich Dec 01 '14 at 18:31
  • the code needs to test the returned value from malloc before using it. Otherwise the code will be dereferencing address 0, which will cause a seg fault event – user3629249 Dec 01 '14 at 18:34
  • the two valid definitions of the main() function are: 1) int main() and 2) int main( int argc, char* argv[]) Your code matches neither of these definitions, so a compiler will raise a warning. Your compile has to have all warnings enabled – user3629249 Dec 01 '14 at 18:36
  • Are you using C or C++? They're not the same, and it's highly unlikely you're programming in both at the same time. Just because the tags are similar doesn't mean they both apply. Answers that are applicable to C++, for instance, may not work in C at all. – Ken White Dec 01 '14 at 18:39
  • Yes, i check returned from malloc, but it is example code and I did not want him to do great – Ivan Ivanovich Dec 01 '14 at 18:43

2 Answers2

2

C++ variant:

strcpy(reinterpret_cast<struct test2 *>(p->ptr)->astr, "hello world");

Also it is worth pointing out, that strcpy function is unsafe and should not be used. Use strcpy_s instead.

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
2

You need to apply -> to the result of the cast (notice the parentheses around the entire cast expression):

strcpy(((struct test2 *)(p->ptr))->astr, "hello world");
printf("%s\n", ((struct test2 *)(p->ptr))->astr);

Live example

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455