Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
problem with memcpy and pointers/arrays confusion - again
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Barry Schwarz, post: 2454291"] There is no prototype in scope for allocate_mem. The compiler is forced to assume it returns int. Error 1 is it doesn't so you invoke undefined behavior. Error 2 is there is no implied conversion between the assumed return type of int and the pointer on the left of the = sign. Error 3 is you apparently chose to ignore the warning this assignment generated. While you are free to ignore warnings (just as compilers are free to produce meaningless ones), you really should know why you are ignoring it before you decide to do so. [] has higher precedence than *. The expression is parsed as *(int_array[N]). int_array is a pointer to int. int_array[N] is the N-th int after the first (which would be int_array[0]). It is not legal to apply the dereference operator to an int. Why did you ignore this diagnostic? double_array is a pointer to double. Once you fix the conflict between the [] and * operators as noted above, the second argument will end up a double. But your %i tells printf to expect an int. Lie to the compiler and invoke undefined behavior. On the second call, you allocate space for N+1 doubles. But since void_ptr is an int*, on systems where doubles are 8 and ints are 4 bytes, you are not storing data in the last double. Furthermore, storing an int bit pattern in a double could possibly generate a trap representation. Furthermore, it would only initialize half the double, the other half is still indeterminate. Back in main, when you try to print the N-th double, you invoke undefined behavior by evaluating an uninitialized variable. Remove del for email [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
problem with memcpy and pointers/arrays confusion - again
Top