Thread: how to advance pointer in a for loop?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    how to advance pointer in a for loop?

    Hello....
    Can someone help me here as I clearly don't know what I am doing:

    1. I would like to know how to create a pointer to the following structure?

    2. Then how to advance the pointer to each member of the structure (same offset)?....so for instance the 2nd element Vbias[0][2], SiPM_Current[0][2], LED_Current[0][0][2], LED_Current[0][1][2]....

    Code:
    typedefstruct {  float Vbias[3][4]; //indexing reading 1/2/3 ; 10/50/100/200 mA
      float SiPMCurrent[3][4];  //indexing reading 1/2/3 ; 10/50/100/200 mA
      float LEDCurrent[3][2][4]; //indexing reading 1/2/3 ; LED 1/2 ; 10/50/100/200 mA
      float w_temp[3][4];  //indexing reading 1/2/3 ; 10/50/100/200 mA
      float bd_temp[3][4]; //indexing reading 1/2/3 ; 10/50/100/200 mA
    
    } t_SenseDataRaw;
    For #1 above I was trying to do the following but VS doesn't like it:

    Code:
    t_SenseDataRawSensorData= {0};float *pSensorData;
    pSensorData = &SensorData.Vbias[0][0];
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can you post your code without the horrible colour scheme from your IDE.

    Copy as text
    Paste as text
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Code:
    t_SenseDataRaw SensorData = {0};float *pSensorData;
    pSensorData = &SensorData.Vbias[0][0];
    
    
    
    
    typedef struct {
      float Vbias[3][4]; //indexing reading 1/2/3 ; 10/50/100/200 mA
      float SiPMCurrent[3][4];  //indexing reading 1/2/3 ; 10/50/100/200 mA
      float LEDCurrent[3][2][4]; //indexing reading 1/2/3 ; LED 1/2 ; 10/50/100/200 mA
      float w_temp[3][4];  //indexing reading 1/2/3 ; 10/50/100/200 mA
      float bd_temp[3][4]; //indexing reading 1/2/3 ; 10/50/100/200 mA
    } t_SenseDataRaw;

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    2) You can't.

    You most probably want to do something like this:

    Code:
    #include <stdio.h>
    
    
    typedef struct {
      float Vbias;
      float SiPMCurrent;
      float LEDCurrent;
      float w_temp;
      float bd_temp;
    } t_SenseDataRaw;
    
    
    t_SenseDataRaw rawData[3][4]; //indexing reading 1/2/3 ; 10/50/100/200 mA
    
    
    int main(void) {
       t_SenseDataRaw *ptr = rawData[2];
       for(int i = 0; i < 4; i++, ptr++) {
          printf("%f\n",ptr->Vbias);
          printf("%f\n",ptr[i].Vbias);  // Same thing
       }
       return 0;
    }
    Last edited by hamster_nz; 03-31-2022 at 02:22 PM.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Could you instead use an index variable (let's call it i as an example) that you use in each member, e.g., SensorData.Vbias[0][i], SensorData.SiPMCurrent[0][i], etc.? Without more information on exactly why you need to have a pointer to the same element in each member, this smells like an XY problem.

    Edit: I like hamster_nz's answer. Consider reorganizing your structure to do what you want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2012, 08:41 AM
  2. Advance function pointer problem.
    By Siaw Ys in forum C Programming
    Replies: 8
    Last Post: 11-19-2011, 02:09 AM
  3. Introduction and Thanks in Advance!
    By Gallaton in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2009, 12:37 AM
  4. need some help thanks in advance
    By Mshock in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 06:44 AM
  5. thanks in advance
    By vijayalakshmi in forum C Programming
    Replies: 32
    Last Post: 02-24-2005, 04:38 PM

Tags for this Thread