[SOLVED] assigning variable to a pointer w/ a type cast
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hmmm, it compiles just fine when I do it. Why don't you show us what you wrote? There is bound to be some typos there.
p = malloc(sizeof(float)); reserves enough memory in the heap to store a floating point number and stores the address of that memory in p.
*p = (float) y; casts (converts) y into a floating point number and stores the result in the memory location pointed to by p.
doesn't the pointer variable p hold the right amount of memory for a float? I'm still trying to figure out pointers but logic tells me that the malloc statement is redundant
The above line reads the value of y and then casts it to (float *) and then writes the result to p.
It is equivalent to:
p = (float*)120;
and i dont think thats what you meant,you probably meant to do:
p = (float*)&y;
The above reads the memory address of variable y and then casts it (float*) and then writes the result to p.
This is probably what you wanted.Use this and it will not segfault anymore.
This makes sense to me but doesn' tdo as expected. Here is the program w/ the output
Code:
#include <stdio.h>
#include <stdlib.h>
int
main()
{
float *p;
//void *p;
int y = 120;
//void *x;
//x = &y;
//p = malloc(sizeof(float));
p = (float*)&y;
printf("p is %f\n", *p);
return 0;
}
compiling this gives the following error:
test1.c:13:26: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
The main difference between our codes is that I included <malloc.h> instead of <stdlib.h>
Quote:
Originally Posted by doughyi8u
doesn't the pointer variable p hold the right amount of memory for a float? I'm still trying to figure out pointers but logic tells me that the malloc statement is redundant
No, p is just a pointer and doesn't hold any memory (other than that required to store a memory address). It has to be initialized to point to a valid memory address before it can be de-referenced. malloc() reserves memory for p to point to.
As mhogomchungu pointed out, p can share the same memory address as y but the bit patterns for integer format and float format are different. If 120 (0x78) is read by a program that interprets it as a floating representation of a number then it will see something with a value of the order of 2^-128 which the printf statement would round off to zero.
[code]
compiling this gives the following error:
test1.c:13:26: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
C language can convert a pointer of type (void*) to a pointer of any other type.
C++ can not and this is one of the differences between C and C++.
What you wrote is a valid C code but invalid C++ code.
You got the error because you are compiling your code using a C++ compiler.
The error will go away if:
1. You compile using a C compiler.
2. You continue using a C++ compiler but cast malloc() to a pointer type of resulting object.
Last edited by mhogomchungu; 08-17-2014 at 12:35 PM.
I want the variable "fp" to print the value copied from the variable "i". Is there another solution?
You are not making a copy of the value. You have declared an int object on the stack, and assigned the value at such location to 40.
You then initialize the float pointer to reference the address of where this int object is stored.
Finally, you attempt to treat the value at this location as a IEEE 754 floating-point value when you call printf(). Perhaps not obvious to you, but the value of 40 in IEEE 754 format is not going to yield the number 40.0.
In conclusion, what you are doing is silly. Yes, you may be learning something on your journey to become a proficient C/C++ developer, but seriously, there is no practical application to what you are attempting to accomplish.
Last edited by dwhitney67; 08-17-2014 at 01:01 PM.
C language can convert a pointer of type (void*) to a pointer of any other type.
C++ can not and this is one of the differences between C and C++.
What you wrote is a valid C code but invalid C++ code.
You got the error because you are compiling your code using a C++ compiler.
The error will go away if:
1. You compile using a C compiler.
2. You continue using a C++ compiler but cast malloc() to a pointer type of resulting object.
Not entirely true; in C++, you can cast a void* to a pointer of another pointer type. You can use a C-style cast, or rely on reinterpret_cast.
In C++, one should NEVER use malloc(). Use the new operator instead.
Not entirely true; in C++, you can cast a void* to a pointer of another pointer type. You can use a C-style cast, or rely on reinterpret_cast.
To be precise with my wording.
C language implicitly converts a pointer of type (void*) to a pointer of any other type,no casting is necessary.
C++ requires explicit casting from (void*) to a pointer of any other type,casting is necessary.
The code is valid C and invalid C++ because it expects the casting to be performed implicitly,something C will do but C++ will not.
Last edited by mhogomchungu; 08-17-2014 at 01:14 PM.
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
They gave their reason here[1],so its an academic exercise aimed at getting a better understanding of how pointers work.
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
The OP already stated what he was trying to achieve:
Quote:
Originally Posted by doughyi8u
I'm trying to *fully* understand pointers
Pointers are the most powerful aspect of C/C++ programming. In fact, languages like Java evolved specifically to reduce the programmer's power in this regard (it's hard to write viruses in Java).
However, getting a grip on the nature of pointers is not an easy thing to do. It doesn't matter much if the exercise is pointless. If it helps the OP in his objective then more power to him.
Well, if assigning random literals to pointers and getting invalid page-faults is a way towards fully understanding pointers, then the mission is complete.
Other (perhaps more fruitful) possibility would be mastering data-structures like lists and trees.
Well, if assigning random literals to pointers and getting invalid page-faults is a way towards fully understanding pointers, then the mission is complete.
Other (perhaps more fruitful) possibility would be mastering data-structures like lists and trees.
Somebody who has yet to understand the role of malloc() is not ready to move on to linked lists.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.