This does not compile under Microsoft using cl (error msg below - what is it complaining about?) but it does using gcc. Can someone explain in plain English the following line? Is there some code with less pointer logic that would do as well?
============================================================
*(char *)(new_node->data + i) = *(char *)(new_data + i);
============================================================
/* Function to add a node at the beginning of Linked List.
This function expects a pointer to the data to be added
and size of the data type */
void push(struct Node** head_ref, void *new_data, size_t data_size)
{
// Allocate memory for node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = malloc(data_size);
new_node->next = (*head_ref);
// Copy contents of new_data to newly allocated memory.
// Assumption: char takes 1 byte.
int i; //line 27
for (i=0; i<data_size; i++) //line 28
*(char *)(new_node->data + i) = *(char *)(new_data + i);
// Change head pointer as new node is added at the beginning
(*head_ref) = new_node;
}
MS error messages
C:\Temp\c\LinkedLists>cl z.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
z.c
z.c(27) : error C2143: syntax error : missing ';' before 'type'
z.c(28) : error C2065: 'i' : undeclared identifier
z.c(28) : error C2065: 'i' : undeclared identifier
z.c(28) : error C2065: 'i' : undeclared identifier
z.c(29) : error C2065: 'i' : undeclared identifier
z.c(29) : error C2036: 'void *' : unknown size
z.c(29) : error C2065: 'i' : undeclared identifier
z.c(29) : error C2036: 'void *' : unknown size
z.c(29) : error C2106: '=' : left operand must be l-value
z.c(74) : error C2143: syntax error : missing ';' before 'type'
z.c(76) : error C2143: syntax error : missing ';' before 'type'
z.c(78) : error C2065: 'arr2' : undeclared identifier
z.c(78) : error C2109: subscript requires array or pointer type
z.c(78) : error C2065: 'float_size' : undeclared identifier
z.c(78) : warning C4022: 'push' : pointer mismatch for actual parameter 2
z.c(78) : error C2198: 'push' : too few arguments for call