1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

how print pointer to a string on LCD

Discussion in 'AVR' started by Azadfalah, Apr 10, 2017.

  1. Azadfalah

    Azadfalah New Member

    Joined:
    Feb 12, 2017
    Messages:
    2
    Likes:
    0
    Hello friends

    how can in print pointer to a string on LCD

    char a[] = "Hello world";
    char *ptr2 = a; // Points to the 'H' of "Hello world"

    how can print *ptr2

    codevisionAVR Atmega64 , alcd.h
     
  2. Daniel Wood

    Daniel Wood Member

    Joined:
    Jun 15, 2012
    Messages:
    69
    Likes:
    9
    Location:
    Derby
    ONLINE
    Strings are generally null terminated.

    Stick it in a while loop with the expression: while(*ptr2 != 0)
    print out each character at a time and then increment the pointer, inside the loop. A bit like:
    printf("%c", *ptr2++);
     
    • Like Like x 1
  3. Daniel Wood

    Daniel Wood Member

    Joined:
    Jun 15, 2012
    Messages:
    69
    Likes:
    9
    Location:
    Derby
    ONLINE
    Might I add, your string must end with a null character (0 in decimal) with the above technique, otherwise you will end up printing data from other areas of memory.

    Another (likely safer) way is to have an integer variable called a_length, with the number of characters specified explicitly.

    Then you print and increment the pointer inside a "for" loop until all the characters have been iterated.
     
    • Like Like x 1

Share This Page