3
votes

I can see this has been asked countless times but I couldn't quite relate the answers I found my individual situation. I'm still getting the hang of C and haven't quite got my head around pointers yet. A basic version of the code I'm having issue with is below:

int function(unsigned long *timestamp)
{
     timestamp = GetTick();
}

Where the GetTick() function returns a value of type uint32_t.

My problem then is that I get a compiler warning that the assignment makes a pointer from an integer without a cast. I understand that pointers and variables are different but I don't fully understand what the meaning of the warning is nor the right way to make the assignment? Any suggestions?

Edit: Might be worth mentioning that I didn't write this code myself, I'm simply trying to port it to use a new set of drivers that should allow it to do the same thing, in theory.


Update:

A cleaner version of the above code:

int function(unsigned long *timestamp)
{
     if (timestamp)
         timestamp = GetTick();
     return 0;
}
4
unsigned int and uint32_t are different types. Your argument to the function should probably be uint32_t timestamp. Or you should cast the return value of GetTick(). Also note that timestamp is a pointer (see answers). - lord.garbage
@brauner: It is perhaps a good idea to prefix a comment which will not actually solve the problem, but improve the code (or correct a minor flaw) with "Note" or similar. About uint32_t I agree, however. - too honest for this site
Is there any reason to pass apointer and not just return the value (whatever it is wort to wrap a function in another function for your project). Note that you do not return a result; if your compiler does not complain, enable warnings (at least -Wall -Wextra -Wconversion for gcc) and compiler in C11 or at least C99 mode. - too honest for this site
There is still the problem with GetTick() returning an int while timestamp is a pointer. But that is, as I said, covered in the answers. - lord.garbage
@brauner: "you should cast" why, please? - alk

4 Answers

4
votes

My problem then is that I get a compiler warning that the assignment makes a pointer to an integer

No, the warning is saying the opposite: you're making an integer into a pointer. That's because GetTick returns an integer but you're assigning the result to timestamp, which is a pointer. You probably meant:

    *timestamp = GetTick();
4
votes

You need to use:

*timestamp = GetTick();

as you have passed pointer and you need to set the value at the memory address pointed to by the pointer.

2
votes

timestamp is a pointer, GetTick() returns a value. You should do

int function(unsigned long *timestamp)
{
     *timestamp = GetTick();
}
1
votes

a pointer is just an address unsigned long *timestamp= NULL. To get the underlying value stored at that address you must de-reference the pointer *timestamp. since you are passing in a pointer to the function if you want to assign a value you must use, in this case, *timestamp = GetTick();

In addition, if(timestamp) will be checking the pointer address. if you want it to check if there is an existing value within the pointer you again need to de-reference, if(*timestamp)