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

  • Home
  • SEARCH
  • 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 6695503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:13:46+00:00 2026-05-26T06:13:46+00:00

I want to create an integer pointer p, allocate memory for a 10-element array,

  • 0

I want to create an integer pointer p, allocate memory for a 10-element array, and then fill each element with the value of 5. Here’s my code:

//Allocate memory for a 10-element integer array.
int array[10];
int *p = (int *)malloc( sizeof(array) );

//Fill each element with the value of 5.
int i = 0;
printf("Size of array: %d\n", sizeof(array));
while (i < sizeof(array)){
    *p = 5;
             printf("Current value of array: %p\n", *p);
    *p += sizeof(int);
    i += sizeof(int);
}

I’ve added some print statements around this code, but I’m not sure if it’s actually filling each element with the value of 5.

So, is my code working correctly? Thanks for your time.

  • 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-26T06:13:46+00:00Added an answer on May 26, 2026 at 6:13 am

    Here’s a more idiomatic way of doing things:

    /* Just allocate the array into your pointer */
    int arraySize = 10;
    int *p = malloc(sizeof(int) * arraySize);
    
    printf("Size of array: %d\n", arraySize);
    
    /* Use a for loop to iterate over the array */
    int i;
    for (i = 0; i < arraySize; ++i)
    {
        p[i] = 5;
        printf("Value of index %d in the array: %d\n", i, p[i]);
    }
    

    Note that you need to keep track of your array size separately, either in a variable (as I have done) or a macro (#define statement) or just with the integer literal. Using the integer literal is error-prone, however, because if you need to change the array size later, you need to change more lines of code.

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

Sidebar

Related Questions

Given a minimum integer and maximum integer, I want to create an array which
In oracle, I want to create a delete sproc that returns an integer based
I want to create a simple integer range checker and converter using c++ templates.
I want to Create a NSMutableDictionary with an Integer Mapping to an strucuter(struct). Example:
I want to create a Qt application that takes a random integer and sends
I want to create a function that will take a string and an integer
i want to create a loop withing my mxml code to create a variable
I want to create a function that takes an integer as it's parameter and
I want to create a stored procedure which takes integer values as a @top
How can i get 0 as integer value from (int)null . EDIT 1: I

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.