Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:51:37+00:00 2026-06-17T22:51:37+00:00

I am writing a C program. I have a data structure called MyStruct that

  • 0

I am writing a C program. I have a data structure called MyStruct that contains a character pointer and also an integer field. I have an array of MyStruct called myArray. What I need is to simply initialize this array by MyStructure structs by initiateParams() function and then read the values of myArray and display them on screen by displayParams().

The initiateParams() function just initializes integer and char* fields of variables of class MyStruct by incrementing integers. After initialization, when I want to retrieve values by displayParams() the value of integer field (intigerData) is retreived correctly; but the value of pointer field (characterSet) displayed incorrectly; maybe the value of last char* value assigned to the last structure. How can I retrieve pointer values correctly? Thank you.

#include <stdio.h>
#include <stdlib.h>
//#include"structHeader.h"
#include <stdint.h>

void initiateParams();
void displayParams();
char* itoa(int value, char* result, int base);


typedef struct _MyStruct
{
    char* characterSet;
    uint16_t intigerData;
    uint16_t * intigerDataPtr;

} MyStruct;

const uint16_t bufferSize  =  400;
MyStruct myArray[400];
int main ()

{    initiateParams();
    displayParams();

    printf("ended");
    return 0;
}

void initiateParams(){
    uint16_t i;
    for(i = 0 ; i < 4*bufferSize ; i++){
        uint16_t k = i % bufferSize;
        MyStruct myStruct;
        myStruct.intigerData = i;
        char c;
        char* c2 = itoa(i , &c , 10);
        c = *c2;

        myStruct.characterSet = &c;
        myStruct.intigerDataPtr = & i;

        //printf("%s\r\n" , &dig);
        myArray[k] = myStruct;
    }
}

void displayParams(){
    uint16_t i;
    for(i = 0 ; i < bufferSize ; i++){
        MyStruct api = myArray[i]; 
        printf("retrieved integer value: %d , character pointer value: %s\r\n" 
                , api.intigerData , api.characterSet);
        printf("%s\r\n" , api.characterSet);
    }
}

/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base) {
    // check that the base if valid
    if (base < 2 || base > 36) { *result = '\0'; return result; }

    char* ptr = result, *ptr1 = result, tmp_char;
    int tmp_value;

    do {
        tmp_value = value;
        value /= base;
        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" 
                [35 + (tmp_value - value * base)];
    } while ( value );

    // Apply negative sign
    if (tmp_value < 0) *ptr++ = '-';
    *ptr-- = '\0';
    while(ptr1 < ptr) {
            tmp_char = *ptr;
            *ptr--= *ptr1;
            *ptr1++ = tmp_char;
    }
    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:51:39+00:00Added an answer on June 17, 2026 at 10:51 pm

    This line

     myStruct.characterSet = &c;
    

    only stores a pointer to the temporary variable ‘c’ which is allocated on the stack.

    You should at least store the value of ‘c2’ variable (without ‘*’) in the characterSet field.

    Also you are not allocating the buffer, passing &c to itoa is erroneous.

    You should (for each character) allocate the buffer:

        char* buf = malloc(10);
        myStruct.characterSet = itoa(i , buf , 10);
    

    and deallocate the structure when you’re done (after displayParams).

    The code should be

    void initiateParams(){
        uint16_t i;
        for(i = 0 ; i < 4*bufferSize ; i++){
            uint16_t k = i % bufferSize;
            MyStruct myStruct;
            myStruct.intigerData = i;
    
            char* buf = malloc(10);
            myStruct.characterSet = itoa(i , buf , 10);
            myStruct.intigerDataPtr = & i;
    
            myArray[k] = myStruct;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains log entries for a program I'm writing. I'm
Im writing a program that should read input via stdin, so I have the
I have a problem with Regular Expressions. I'm writing a small program that matches
I am writing a program that creates a stack using linked lists. I have
I have a program which creates a C structure which contains large arrays of
I am writing a program in Python, that reads the YV12 Frame data from
I have some questions with a particular structure of a program I'm writing. I'm
Writing a program that should be portable in Linux and Windows enviroments I have
I'm writing a program that reads and processes large amounts of data. To speed
I'm writing a program in Java and I have a method with a header

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.