-4

what is different between this:

tElemPtr novyPrvok = (tElemPtr *)malloc(sizeof(tElemPtr));

and this:

tElemPtr novyPrvok = malloc(sizeof(tElemPtr));

I want to use it in InsertFirst function for inserting first element in the beginning of the List. Because, if I use without that pointer in front of malloc, Xcode tells me it is good, but I cant use "novyPrvok->data"

void InsertFirst (tList *L, int val) {  
    tElemPtr novyPrvok = (tElemPtr *)malloc(sizeof(tElemPtr));

    if(novyPrvok == NULL)
        Error();

    novyPrvok->data = val;
    novyPrvok->ptr = L->First;
    L->First = novyPrvok;              
}
  • 1
    you meant tElemPtr *novyPrvok = (tElemPtr *)malloc(sizeof(tElemPtr)); ? – OznOg Oct 01 '18 at 18:54
  • 2
    @OznOg tElemPtr - it is probably typedef-ed pointer. – 0___________ Oct 01 '18 at 18:55
  • show *failing* code and exact error message – pm100 Oct 01 '18 at 18:55
  • 2
    then the cast and the sizeof() are wrong – OznOg Oct 01 '18 at 18:55
  • 1
    typedefing pointers is a horrible habit, leading to difficult to read code. Do bot do it. The only exeption: function pointers – 0___________ Oct 01 '18 at 18:56
  • @P__J__ Function pointers are no exception. If you don't like the syntax, you can just typedef functions. – melpomene Oct 01 '18 at 18:57
  • Casting malloc()'s return value is not something that needs to be, or should be, done. http://c-faq.com/malloc/mallocnocast.html – Shawn Oct 01 '18 at 18:57
  • @melpomene function pointers are usually used another way then the "normal" pointers. – 0___________ Oct 01 '18 at 18:58
  • @Shawn it is a prehistoric opinion. C standard does not allow functions with no prototypes (it is the error now not the warning - and you cant compile the code the program with no included ), so that link is about 20years outdated. Obsolete opinion. Now it only question of taste – 0___________ Oct 01 '18 at 19:01
  • They are similar in a way that neither former nor the latter makes any sense. Check your indirection levels carefully. – AnT stands with Russia Oct 01 '18 at 19:07
  • 1
    @P__J__ You'll find that many compilers, including gcc and clang, will complain about it but still compile programs that use malloc without including the appropriate header. – Shawn Oct 01 '18 at 19:13
  • @Shawn just use the correct compile option to set the modern language standard. – 0___________ Oct 01 '18 at 19:38
  • @P__J__ Even with C11 (Which is the default in recent versions of gcc, not sure about clang), they will compile such code. Don't believe me? Try it yourself with a file with nothing but `int main(void) { int *x = malloc(sizeof *x); }` in it and compile with `gcc -std=c11`. You'll get some warnings, but it *will* compile (Tested with gcc 7 and 8, and clang 6). If that's not enough to demonstrate that, yes, compilers still let you use functions without declaring them first, well... I'm done. Peace. – Shawn Oct 01 '18 at 20:37
  • hi, please can you tell me, how can I acces data, when I have it declared linke pointer? (*tElemPtr) typedef struct tElem { struct tElem *ptr; int data; } *tElemPtr; typedef struct { tElemPtr Act; tElemPtr First;} tList; – Timco Vanco Oct 02 '18 at 17:33

2 Answers2

1

no need to cast malloc return -> Do I cast the result of malloc?

BTW your code seems wrong; as tElemPtr seems to be a typedef on a pointer, I would expect the malloc to be:

tElemPtr novyPrvok = malloc(sizeof(*novyPrvok));
OznOg
  • 4,440
  • 2
  • 26
  • 35
-1

your malloc is wrong, its only allocating space for a pointer. After that everything is Undefined Behavior. Should be

 tElemPtr novyPrvok = (tElemPtr *)malloc(sizeof(*tElemPtr));
pm100
  • 48,078
  • 23
  • 82
  • 145
  • You're right, but the code sample isn't. `tElemPtr novyPrvok = malloc(sizeof *novyPrvok);` – Shawn Oct 01 '18 at 19:00
  • You're still only allocating space for a pointer. Or at least you would be if `*tElemPtr` wasn't a syntax error. – melpomene Oct 01 '18 at 19:00