Usin a RAW value of a variable.

Hi,

I'm trying to make a memory log of several variables in a flexible way. I need to pass a variable to a function, but only the position of first byte an the leg of this. I make this function, but not work...

  unsigned int temp = analogRead(A0);
  Registros.Concatenar(&temp, 2);

void Registro::Concatenar (char* Cadena, uint8_t Largo) {
  for (uint8_t i = 0; Largo > i; i++) {
    _RegistroAuxiliar[_RegistroOcupado] = Cadena[i];
    _RegistroOcupado++;
  }
}

But I have this error:

note: no known conversion for argument 1 from ‘unsigned int*’ to ‘char*’

How can I do this?

Thanks

Leonel

It means what it says.

You have defined the function to receive a char* as the first argument.

And then you try to invoke the function with an int* as the first argument.

I am dubious about the compilers claim that a pointer is not a pointer. It's a pointer.

But anyway, you need to consider what you are trying to do. Do you want the function to have a char, or an int ? Make up your mind.

mmmm, I need the memory position (pointer) of the first byte is the same if I have a Int or a long, the first byte is the same in 2 variable types.

How can I pass a pointer (just a pointer) of this variable.

The other option is make a new function to each variable type, but is a lot of redundant code.

Thanks.

...I need the memory position (pointer) of the first byte is the same if I have a Int or a long...

Wrong. A pointer is a data type that can point to a specific type of data (int, char, float). The rvalue of a pointer is the lvalue of the data item being pointed to. Those are not the same thing. The statement:

a = b;

takes the rvalue of b and places it in the rvalue of a. However, with a pointer:

ptr = &a;

takes the memory address of a (i.e., its lvalue) and assigns it into the rvalue of ptr. A valid pointer can only hold one of two values: 1) the lvalue of a variable of a specific data type, or 2) null. When you define ptr, you must tie it to some type of data, even if it is void.

I have a small solution, I'm trying this...

And then you try to invoke the function with an int* as the first argument.

No. OP is invoking the function with the address of a variable. The address of a variable is not the same as a pointer to the variable.

"void*" is the usual preferred anonymous pointer type.

Do this...

Registros.Concatenar((char *)&temp, 2);

econjack:
A valid pointer can only hold one of two values: 1) the lvalue of a variable of a specific data type, or 2) null.

I don't think that's quite accurate. What it means for a pointer to be 'valid' is open for debate, but if you mean that the pointer can be safely dereferenced then arguably null is not a valid value (unless you are actually accessing data at address zero), but the pointer is valid if it holds the address of any memory location where the pointed-to type can be accessed - it doesn't need to be the address of a variable. For example, if you are storing data in heap memory then it would be normal to use pointers to access that but there would be no corresponding variables at those locations.

. . . or it could be a function pointer