Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: need help understanding pointers and returning an array of bytes

  1. #1

    need help understanding pointers and returning an array of bytes

    Gentlemen,
    I have multiple questions that are relative to each other. The first question involves pointers and how they construct / destruct... See code example below.
    Code:
    uint8_t * P;
    
    void do_something(uint8_t len, uint8_t my array[])  {
      for(uint8_t i = 0; i<len; ++i) {* P++ = my array[a];
    }
    I have read that you can increment pointers through what I call memory blocks (similar to arrrays[]) by the following example above. I have a few questions regarding declarations and clearing out pointers...
    1. If I declare a pointer "P" as GLOBAL, does the pointer "P" destruct itself every time @ the functions end life cycle?
    2. If I declare a pointer "P" as LOCAL with-in the function, does the pointer "P" destruct itself every time @ the functions end life cycle? Upon function re-initilization, does the pointer re-instate itself and "NULL" with no data?
    3. If I declare a pointer "P" as STATIC GLOBAL, does the pointer "P" stay in memory and retain its old data independent of the functions life cycle, but can be shared among functions?
    4. If I declare a pointer "P" as STATIC LOCAL with-in a function, does the pointer retain its data only with-in its function, never destructs, and does not "NULL"?

    If question #4 does not NULL.... how does "NULL" work when using pointers?
    Does a pointer "NULL" similar to the example below...
    Code:
    void some_function{} {...
      static uint8_t * P;
      P = NULL;
    }
    clears out a pointer like...
    Code:
    P[0] = NULL;
    P[1] = NULL;
    P[2] = NULL;
    ...
    or does it just null the first memory block only and leaves the remainder memory blocks with left over data like...
    Code:
    P[0] = NULL;
    ...
    P[1] = 0x02;
    P[2] = 0x04;
    Could someone please clarify.

    Another question. I want to create a function similar Serial1.readbytes(array, len); See example code below...
    Code:
    uint8_t get_data() {
      static uint8_t command_send[4] = {0xfe, 0xc1, 0x35, 0x31};
      static uint8_t command_receive[4];
      Serial1.write(command_send, 4);              //send out a command
      delay(25);                                               //give the other side time to respond
      Serial1.readbytes(command_receive, 4);   //receive incoming data
      return command_receive[3];                   //return only what is need, throw away what is not
    How does Serial1.readbytes() take the true array command_receive[] within its function and returns the data in the same array without having to use a pointer called command_receive to return the code...
    Code:
    uint8_t * command_receive;
    command_receive = Serial1.readbytes(command_receive, 4);
    I want to retain the data in array command_receive[] for use upon the same functions re-initialization. This will allow me to perform an additional task in the place of delay(25) and actually reduce to delay(15). See below for example code...
    Code:
    uint8_t get_data() {
      static uint8_t command_send[4] = {0xfe, 0xc1, 0x35, 0x31};
      static uint8_t command_receive[4];
      Serial1.write(command_send, 4);              //send out a command
      Serial3.write(command_receive[3], 1);     //print result onto LCD;
      ... perform some other commands....
      delay(15);                                               //give the other side time to respond
      Serial1.readbytes(command_receive, 4);   //receive new incoming data
      return command_receive[3];                   //return the new data, throw away what is not needed
    My purpose is to first confirm my interpretation of pointers. I also want to have some understanding how readbytes(##, 2) is coded with some sort of an example without the dependancy of Serial3 class... just readbytes alone.

    Thanks in advance
    BLMinTenn

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    208
    So many questions. I will try to answer a few but you should look for a good tutorial on pointers.

    You have questions about the scope(global, static, auto) of pointers and they act like any other variable.

    p = NULL; does not do anything useful.

    I am not sure what exactly you are trying to do with your other code.

    Pointer Example:

    Code:
    char letters[] = "hello ";
    
    char *p;        // a pointer is declared but not usable yet
    
    p = letters;         // now the pointer points to the variable letters or you could say
    p = &letters[0];   // now it points to the first element in the letters array which is the same thing
                           
      switch(letters);   // you could call some function  using the array name
      switch(p);          // or use the pointer
    
                              // the function might be written
    void switch( char var[] ){
    char c;
           c = var[0];
           var[0] = var[4];
           var[4] = c;
    }
    
          or written
    switch( char *v ){
    char c;
    
          c = *v;
          *v = *(v+4);
           v += 4;       // change the pointer just for fun
          *v = c;        // where it points is now different
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •