Om Choudhary
Om Choudhary

Reputation: 512

c how do you pass pointer by reference and increase the value

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;
}

Upvotes: 1

Views: 125

Answers (2)

Amrito
Amrito

Reputation: 25

Changing D_SetStat function to have parameter of a pointer of type uint16_t and then derefence and incrementing it seems to work.

void D_SetStat(uint16_t* pPointer)
{
    *pPointer += UINT16_C(0x0001);
}

But getting a warning for const declaration:

warning: passing argument 1 of 'D_SetStat' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

Update:

After declaring a constant pointer to constant, you cannot change what it's pointing at nor it's value later, as said here.

Though I'm not clear whether this answers your question, you can change D_SetStat function in the following way.

As you have many members in S_Stats_t struct, pass an int to D_SetStat function and it'll update the corresponding member.

void D_SetStat(uint16_t number)
{
    switch(number)
    {
        case 0: // Incrementing C_Signal.Stat, assuming Stat is the 0th member
            C_Signal.Stat += UINT16_C(0x0001);
            break;
        case 1: // Update other members
            ...
            ...
        default: // In case an unknown number is passed
            ...
    }
}

And then in main function call D_SetStat like this:

    D_SetStat(0); // The member number you want to update

Upvotes: 1

Hugo Cunha
Hugo Cunha

Reputation: 126

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;
}

Upvotes: 2

Related Questions