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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:30:44+00:00 2026-05-26T02:30:44+00:00

I have the following structure: struct CountCarrier { int *CurrCount; }; And this is

  • 0

I have the following structure:

struct CountCarrier
{
    int *CurrCount;
};

And this is what I want to do:

int main()
{
    CountCarrier carrier = CountCarrier();
    *(carrier.CurrCount) = 2;  // initialize the *(carrier.CurrCount) to 2
    IncreaseCount(&carrier);  // should increase the *(carrier.CurrCount) to 3
}


void IncreaseCount(CountCarrier *countCarrier)
{
    int *currCounts = countCarrier->CurrCount;
    (*currCounts)++;
}

So, my intention is specified in the comments.

However, I couldn’t get this to work. For starters, the program throws an exception at this line:

*(carrier.CurrCount) = 2;

And I suspect the following line won’t work as well. Anything I did 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-05-26T02:30:44+00:00Added an answer on May 26, 2026 at 2:30 am
        struct CountCarrier 
        {     
            int *CurrCount;  //No memory assigned
        }; 
    

    You need to allocate some valid memory to the pointer inside the structure to be able to put data in this.

    Unless you do so, What you ar trying to do is attempting to write at some invalid address, which results in an Undefined Behavior, which luckiy in this case shows up as an exception.

    Resolution:

        struct CountCarrier 
        {     
            int *CurrCount;  //No memory assigned
            CountCarrier():CurrCount(new(int))
            {
    
            }
        }; 
    

    Suggestion:
    Stay away from dynamic allocations as long as you can.
    When you think of using pointers always think whether you really need one. In this case it doesn’t really seem that you need one, A simple int member would be just fine.

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

Sidebar

Related Questions

gcc 4.4.4 c89 I have the following structure. struct device_sys { char device[STRING_SIZE]; int
Say we have the following structure: struct Tree { string id; int numof_children; Tree
I have following query: THis is my structure in some .h file typedef struct
I have the following structure: private struct S_indiv { public int[] x; public int
I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I'm using C++, and I have the following structures: struct ArrayOfThese { int a;
While implementing a FIFO I have used the following structure: struct Node { T
Let's say I have the following structure declaration (simple struct with no constructor). struct
I have the following array structure (linked list): struct str_pair { char ip [50]
I have the following structure, typedef struct _MainWin { GtkWindow parent; GtkWidget* scroll; GtkWidget*

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.