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 7770557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:26:15+00:00 2026-06-01T16:26:15+00:00

okay, this is probably really simple, but i looked all over the site about

  • 0

okay, this is probably really simple, but i looked all over the site about pointers, arrays, and memory allocation, but some explanations are a bit too complex for me. So… could someone explain to me why d[i] are pointing to different things in the below code?

typedef struct data_t {
    int ival;
    char *sval;
} data_t;

void f1(data_t **d);
int main()
{
data_t *d; 
    d = new data_t[500];
    for (int i=0; i<500; i++)
    {
        d[i].ival= i+1;
        d[i].sval="$";
    }

    f1(&d); 
}

void f1(data_t **d)
{
    for (int i=0; i<500; i++)
    {
        d[i]->ival=i+1;
        d[i]->sval="$";
    }
}

though what i need to do is fill in each of the 500 elements of the array such that the integer field ‘ival’ has the values 1-500 for array indexes 0-499, and the string field ‘sval’ contains the string “$1” – “$500” for array indexes 0-499. in the function call.

also: is

d = new data_t[500]

all i need to allocate memory for a 500 element array of structures?

just trying to figure out why it works in the for loop in main, but not in the function call… looking at the debugger the pointers are pointing at something way wrong…

  • 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-01T16:26:16+00:00Added an answer on June 1, 2026 at 4:26 pm

    “It works” in main because d[i] is the same as *(d + i). In your function f1, d is a pointer to a pointer, so you need to say (*d)[i].ival etc. (What you wrote, d[i], is advancing the pointer to the pointer by i steps and then tries to dereference that, which is the wrong way round.)

    Then again, since you’re in C++, you should probably just have passed the pointer as a reference:

     void f2(data_t * & d)  // <--- reference to the original pointer
     {
         // ...
         d[i].ival = i + 1;
     }
    

    Or actually, since you’re never changing the original d itself, only what it points to , you can even pass it by value:

    void f3(data_t * d)     // <--- pointer passed by value
    {
         // as before, d[i].ival etc.
    }
    

    Or rather, you should probably use a std::vector<data_t> and not use any pointers at all. (Also, you shouldn’t typedef your class definition of data_t.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm probably missing something really simple here, but I've been at this for
Okay, this is (probably) a very simple question, but I am afraid I know
There is probably a really simple answer to this but I just can't see
Okay, I know this is probably dead simple, but I can't seem to find
Okay, so I probably shouldn't be worrying about this anyway, but I've got some
Okay this is probably a really dumb question, however it's really starting to hurt.
okay so this is probably a soft pitch question for sombody, but I want
Okay, this is probably a very basic question; but, I'm just getting back in
Okay this may be a simple question but I have yet to come with
1. Okay this is probably lame question, but I haven't found good result searching

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.