Thread: modify value inside array of struct

  1. #1
    Registered User
    Join Date
    Dec 2022

    modify value inside array of struct

    Greetings from a new member. I am so glad that I found this forum, which is an excellent source for learning. Can someone help me with this task:

    I have an array of structs:

    typedef struct {
        int id;
        String topic;
        float value;
    // then create array of struct and add some values
    myStruct myArray []= {
        {1, "topic1", 26},
        {2, "topic2", 27},
        {3, "topic3", 31},
    In rest of my code I have another varibales with fresh value (it changes in a certain period of time), lets say float topic1freshvalue....
    What I'm trying to do, but I don't know if it's possible, is to create a pointer inside the structure (*myStruct.value) that will point to the variable outside, let say ptr-variable, then to assign a value, IE

    so that whenever the topic1freshvalue variable changes its value, the same is reflected on the value in the structure.
    In short, I want to change the value of topic1 from 26 to new value that hold topic1freshvalue float variable.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    To be honest, you should find a way that doesn't involve pointers.

    It's not that pointers can't be used - they can.
    But if you mess it up (and you will), trying to debug a pointer mess is hard work.

    void updateByID(int id, float value) {
      // search myArray for matching id
    void updateByTopic(String topic, float value) {
      // search myArray for matching topic
    So that when you update topic1freshvalue, you call a function.
    /// or
    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
    Dec 2022
    Thanks a lot Salem, actually that's the advice I was looking for, I was on the wrong track with pointers.
    The most important thing is to find the correct way to solve a problem.
    I followed your advice completely and made a function (only three lines) that refreshes the value when necessary. Now the complete code works great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expand a 2D char array inside a Struct
    By filzek in forum C Programming
    Replies: 1
    Last Post: 03-03-2020, 10:48 PM
  2. Dynamic string array inside a struct.
    By RagingGrim in forum C Programming
    Replies: 2
    Last Post: 05-24-2016, 11:29 AM
  3. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  4. array inside struct
    By tat in forum C Programming
    Replies: 15
    Last Post: 11-13-2007, 12:36 PM
  5. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM

Tags for this Thread