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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:44:19+00:00 2026-06-12T08:44:19+00:00

I am learning how to create dynamic 1D arrays in C. The code below

  • 0

I am learning how to create dynamic 1D arrays in C. The code below tries to do the following:

  1. Using malloc, create a dynamic array of length 10, that holds values of type double.
  2. Set each entry of the array to j/100 for j = 0, 1,..., 9. Then print it out.
  3. Add an additional empty entry to the end of the array using realloc.
  4. Set the new entry to j/100 and print out each entry again.

Testing:

 double* data = (double*)malloc(10*sizeof(double));

 for (j=0;j<10;j++)
 {
      data[j]= ((double)j)/100;
      printf("%g, ",data[j]);
 }

 printf("\n");

 data = (double*)realloc(data,11*sizeof(double));

 for (j=0;j<11;j++)
 {
     if (j == 10){ data[j]= ((double)j)/100; }
     printf("%g, ",data[j]);
 }

 free((void*) data);

Questions

  1. Am I coding this right?

  2. Tutorials I found use malloc without putting the (double*) in front. E.g.,

    int *pointer;
    pointer = malloc(2*sizeof(int));

This does not compile for me on Visual Studio 2010, Windows 7. The error message is

value of type void cannot be assigned to entity of type int.

Why does it work for those tutorials and not for me? Am I right to guess that it is because the compilers they are using automatically fill in the (int*) for them in my example?

  • 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-12T08:44:20+00:00Added an answer on June 12, 2026 at 8:44 am

    You’re close.

    In C (at least since the 1989 version of the standard), the cast before malloc and realloc is unnecessary, since C can convert values of type void * to int * without a cast. This is not true for C++, so based on the error you’re getting, it sounds like you’re compiling this code as C++ and not C. Check the documentation for VS2010 to determine how to compile code as C.

    The following is my preferred style for writing a malloc call:

    double *data = malloc(10 * sizeof *data);
    

    Since the type of the expression *data is double, sizeof *data is equivalent to sizeof (double). This also means you don’t have to adjust your malloc calls if the type of data changes.

    As for the realloc call, it’s safer to assign the result to a temporary pointer value. realloc will return NULL if it cannot extend the buffer, so it’s safer to write

    double *tmp;
    ...
    tmp = realloc(data, 11 * sizeof *data);
    if (!tmp)
    {
      // could not resize data; handle as appropriate
    }
    else
    {
      data = tmp;
      // process extended buffer
    }
    

    Be aware that Microsoft’s support for C ends with the 1989 version of the language; there have been two revisions of the language standard since then, which have introduced some new features and deprecated old ones. So while some C compilers support C99 features like mixed declarations and code, variable length arrays, etc., VS2010 will not.

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

Sidebar

Related Questions

I'm learning to create a WMS service using MapServer and after that I want
I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET
I am learning to create RESTful services using WCF. There are a myriad of
I am learning how to create an android app and webservice using: Eclipse IDE
I learning Perl and I want to create a simple application that gets all
I have the following code to encrypt a value (listed below). Now I would
I'm trying to learn how to create dynamic websites using Glassfish (a Java application
I understand that the idea is to create basic HTTP Requests using GET or
I am learning to create a website and using a pre-created HTML template so
I am learning to create a website using google app engine with python and

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.