Ad

C How Do You Pass Pointer By Reference And Increase The Value

- 1 answer

I know its a stupid question, but any help here is appreciated :)

So I want to pass the pointer by reference in C, and would like to increase the value

""""""""""File: stat.h""""""""""""""""""
typedef struct
{
    uint16_t  Stat;
}S_Stats_t;

""""""""""File: stat.c""""""""""""""""""
static S_Stats_t C_Signal =
{
    .Stat = 0x0001,
};

// Get the stat
const S_Stats_t* D_GetSignal(void)
{
    return &C_Signal;
}

// increment stat
void D_SetStat(S_Stats_t* pPointer)
{
    pPointer=pPointer+UINT16_C(0x0001);
}

""""""""""File: main.c""""""""""""""""""
#include <stdio.h>
#include <stdint.h>
#include <stat.h>

int main()
{
    // Get the current stat
    const S_Stats_t* const pSignal = D_GetSignal();
    // increment the stat
    D_SetStat(&pSignal->Stat);
    // Print the new stats
    printf("incremented Counter value, n:0x%x - %u.\n",pSignal->Stat,pSignal->Stat);

    return 0;
}
Ad

Answer

It's not very clear what you want to do but I'm assuming that you want to increment the Stat member of the C_Signal struct, by passing the pointer to the struct.

First of all, if you want to modify the value, you should remove the const qualifier. The const qualifier should be applied to the declaration of any variable to specify that its value will not be changed.

In the D_SetStat function, you want to increment the Stat member, not the struct pointer.

stat.h

#include <stdint.h>

typedef struct
{
    uint16_t  Stat;
}S_Stats_t;


S_Stats_t* D_GetSignal(void);


void D_SetStat(S_Stats_t* pPointer);

stat.c

#include "stat.h"

static S_Stats_t C_Signal =
{
    .Stat = 0x0001,
};

// Get the stat
S_Stats_t* D_GetSignal(void)
{
    return &C_Signal;
}

// Increment stat
void D_SetStat(S_Stats_t* pPointer)
{
    pPointer->Stat += (uint16_t)0x0001;
}

main.c

#include <stdio.h>
#include <stdint.h>
#include "stat.h"

int main(void)
{
    // Get the current stat
    S_Stats_t* pSignal = D_GetSignal();
    // Increment the stat
    D_SetStat(pSignal);
    // Print the new stats
    printf("incremented Counter value, n:0x%x - %u.\n",pSignal->Stat,pSignal->Stat);

    return 0;
}

EDIT:

I don't know why you need to keep the const qualifier. It doesn't make much sense to modify a const variable and it will be discarded by the compiler anyway. You can avoid the warning message by making the explicit cast (see below).

stat.h

#include <stdint.h>

typedef struct
{
    uint16_t  Stat;
}S_Stats_t;


const S_Stats_t* D_GetSignal(void);


void D_SetStat(uint16_t* pPointer);

stat.c

#include "stat.h"

static S_Stats_t C_Signal =
{
    .Stat = 0x0001,
};

// Get the stat
const S_Stats_t* D_GetSignal(void)
{
    return &C_Signal;
}

// Increment stat
void D_SetStat(uint16_t* pPointer)
{
    *pPointer += (uint16_t)0x0001;
}

main.c

#include <stdio.h>
#include <stdint.h>
#include "stat.h"

int main(void)
{
    // Get the current stat
    const S_Stats_t* const pSignal = D_GetSignal();
    // Increment the stat
    D_SetStat(&((S_Stats_t*)pSignal)->Stat);
    // Print the new stats
    printf("incremented Counter value, n:0x%x - %u.\n",pSignal->Stat,pSignal->Stat);

    return 0;
}
Ad
source: stackoverflow.com
Ad