I need some pointers with pointers.
What is the difference between a variable preceded by an "*" and an "&"?
Some of my sample code uses one and some the other. I'm getting compiler errors trying both saying the variable is not defined. First use this function.

When declaring a variable, preceding the variable name with a * makes it a pointer. Preceding it with a & is illegal (in C - in C++ it makes the variable a reference).

In an expression & takes a variable and produces a pointer to that variable, whereas * takes a pointer and produces the value that that pointer points to.

What sepp2k said is quite correct, but difficult to understand for noobs. Here is possibly a more understandable example:

int anInt = 5;
int* ptrToAnInt = &anInt;

anInt += 2;
printf("anInt and ptrToAnInt == %d (%d)\n", anInt, *ptrToAnInt);

This should result in the output "anInt and ptrToAnInt == 7 (7)".

Clear as mud yet? :-)

It's a lot clearer than the Mississippi mud I was looking through before. Thank you sepp2k and rubberman!

Now I get "warning: initialization from incompatible pointer type"

Here's what I did:

        INT32* pVoltage = &Voltage_High;
        CalVal = CALCOR(pVoltage);

The way I understand it, pointers are addresses so should be INT16 or INT32 depending on the processor used, even though Voltage_High is type float.

It may also help if you see & as an address of operator.
o in this statement: INT32* pVoltage = &Voltage_High; you define a pointer to an int32 and you give it an address of a float. I guess the compiler is not happy with that. int32 is 4 bytes and a float is normally 8 bytes in memory.

If Voltage_High is of type float, then &Voltage_High is of type pointer-to-float. The fact that all pointers are the same size in memory is irrelevant. If you want to point to a float, you need a float-pointer;

float* pVoltage = &Voltage_High;

commented: Quite right! +15

It didn't work that way (didn't accept float*) so I was thinking that all pointers were of type INT because the address of the pointer is 32 bits in a 32 bit processor. So it should always by of the type that the pointer is pointing for?

    *ptr = (unsigned int*) &PortBuffer;
    SpiChnPutS(Chan, *ptr, pNumBytes);

gives me "warning: assignment makes integer from pointer without a cast". What's a cast?

int i = 65;
/* good compilers wont accept this, an int is not a char */
char c = i; 
/* cast an int to char */
char c = (char)i; 

In the last line, you are telling the compiler: "He, I know what I'm doing, it is OK." c should now be equal to 'A'. But casting does not always work to change one type to another. Casting a float to a char, will not work.

So I want to use UINT32 &PortBuffer? UINT32 is defined somewhere else to be an unsigned 32 bit integer. I need to use the value of the PortBuffer in a function in another file.

Then you cpuld cast the PortBuffer pointer to a UINT32. Don't really know your intentions, do you want to cast the address of the PortBuffer or the contents of the address the PortBuffer is pointing to?

I still don't know what a cast is. But, I'm trying to point to the address of the contents of PortBuffer. PortBuffer could be anywhere from one byte to 16 bytes.

On this port, I have to send 16 bit data, 24 bit and 32 bit data 8 bits at a time. I have a 32 bit processor that transfers 8, 16 or 32 bits at a time. There is another processor that will do 24 (type short long) bits but won't do 32. Another question...how do I access the bytes from the pointer at the other end?

If you use a byte pointer, you should be able to "walk" through 32-bits, using pointer arithmetic. Her is an article about the subject.

I had just gotten an older book that explained pointers fairly well. I was correct in thinking all pointers were the same size for a particular processor but the reason for saying "char *ptr" or "float *ptr" is to set the size of item each pointer is pointing to. Otherwise type int is assumed.
This article explained it the rest of the way. Thank you very much!

For accessing the bytes from the other end do I...

    INT16 correction(float *voltage);  // prototype
    INT16 correction(float *voltage)   // function
    {
        INT8 ptr = *voltage;
        .
        .
        .
    }

INT8 is defined elsewhere as type char and could easily be called char since type char is always 8 bits. INT16 is defined as a 16 bit integer appropriate for the particular processor. It's called INT16 in the program for portability.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.