2

I am trying to pass a String to a function. I have read a lot about RAM/ROM Strings in C18 and my code seems ok but it is not working. My function is like this:

int setBluetoothName (static const rom char *name){
    unsigned int n = 0, k = 0;
    char string[27] = {0};    // ATNAME + 20 caracteres de nombre + EOF
    char command[27] = {0};
    _bit state = USART_INT;

    // Deshabilito interrupciones del USART
    USART_INT_DISABLE;
    strcpypgm2ram(&string, &name);

    // Numero de caracteres del nombre
    for (; string[n] != 0x00 ; ++n);
    if(n > 19) return LENGTH_ERROR;     // si me pase de 20 letras es muy largo

    // Configuro el nombre del Bluetooth
    printf("ATNAME%s", &string);

And I use it on this way:

setBluetoothName("Brazo");

In Proteus I see that only the letter 'B' is being passed and when I copy the ROM string to RAM it is simply a mess (see the attached picture). The output of the printf() is only ATNAME, the string is not being printed.

I am woking with C18 v4.40 and MPLABX v1.41. Thank you very much for your help.

enter image description here

Andres
  • 6,080
  • 13
  • 60
  • 110

1 Answers1

2

Try:

strcpypgm2ram(string, name);

and

printf("ATNAME%s", string);

when you declare an array

char string[27] = {0};

then the variable string refers to the address of the first element of the array, and when you declare a parameter such as

int setBluetoothName (static const rom char *name)

then name refers to the address where the string is located.

When you add an & in from of these, you are getting the address of the variable containing the address of the data.

Jay Elston
  • 1,978
  • 1
  • 19
  • 38
  • Nice, I didn't know that: When you add an & in from of these, you are getting the address of the variable containing the address of the data. That is in C ? Or just a C18 standard ? I will try it when I get home. – Andres Jan 31 '13 at 01:45
  • @Andre -- Yes, it has been a feature of C when Dennis Ritchie defined the language. – Jay Elston Jan 31 '13 at 06:54