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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:44:21+00:00 2026-05-16T17:44:21+00:00

In my program I am trying to resize array using malloc function. #include <stdio.h>

  • 0

In my program I am trying to resize array using malloc function.

#include <stdio.h>

int main(void)
{
    int list[5],i;
    int* ptr = &list;

    for(i = 0; i < 5; i++)
        list[i] = i;

    for(i = 0; i < 5; i++)
        printf("%d\n", list[i]);

    printf("----------------------------------------\n");

    ptr = malloc(10);

    for(i = 0; i < 10; i++)
        list[i] = i;

    for(i = 0; i < 10; i++)
        printf("%d\n", list[i]);
}

While compiling the program I get two warnings :

searock@searock-desktop:~/C$ cc malloc.c -o malloc
malloc.c: In function ‘main’:
malloc.c:6: warning: initialization from incompatible pointer type
malloc.c:16: warning: incompatible implicit declaration of built-in function ‘malloc’

My program is running fine. I can’t understand why the compiler is giving me this errors?

Should I change my approach?

Edit 1 : And then how do I free the memory? should I use free(list); or free(ptr);

Edit 2 : Updated Code

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int list[5],i;
    int* ptr = malloc(5 * sizeof(int));   //&list;

    for(i = 0; i < 5; i++)
        ptr[i] = i;

    for(i = 0; i < 5; i++)
        printf("%d\n", ptr[i]);

    printf("----------------------------------------\n");

    ptr = realloc(ptr, 10 * sizeof(int));    //malloc(10);

    for(i = 0; i < 10; i++)
        ptr[i] = i;

    for(i = 0; i < 10; i++)
        printf("%d\n", ptr[i]);

        free(ptr);
}

Thanks.

  • 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-16T17:44:22+00:00Added an answer on May 16, 2026 at 5:44 pm

    You aren’t actually reallocating list. list is still 5 bytes, while ptr points to the 10 byte array.

    Do something like this instead:

    int* list = malloc(5 * sizeof(int));
    ...
    list = realloc(list, 10 * sizeof(int));
    ...
    

    Now, when you say:

    int* ptr = &list;
    

    You aren’t creating a “reference” to list; in this context (not in general), it’s the same as:

    int* ptr = list;
    

    This means ptr[3] and list[3] are the same int, but making ptr point to a new buffer won’t make list point to a new buffer as well. If this were C++, the syntax for declaring ptr the way you’re thinking would be (I think):

    int (&ptr)[5] = list;
    

    In any case, you can’t realloc an automatically-allocated buffer, anyway. This won’t work:

    int buffer[5];
    buffer = realloc(buffer, 10 * sizeof(int));
    

    The realloc would likely cause a segmentation fault, and assigning the new pointer to buffer isn’t allowed by C.

    Finally, stealing from the other answers, you need to #include <stdlib.h> to use malloc and friends; otherwise, it’ll be implicitly declared to return an int rather than a pointer, yielding warnings and screwing up 64-bit compatibility for no good reason.

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

Sidebar

Related Questions

Using glew, I'm trying to link the simple program #include </usr/include/GL/glew.h> int main (int
I'm trying to program a graph class using an adjacent list from an example
I've been writing a program trying to find itself using the procps library. But
I'm trying to program my first GUI-class in Java, using the Window Builder. Ok,
I'm trying to program ARM using Eclipse + CDT + yagarto (gnu toolchain) +
I am trying a program with Swing. I am using a socket to connect
I am trying to create a fullscreen chat program using Flash AS3, and so
I'm trying to achieve a dynamic two dimensional array in C. Whenever the program
Okay, so I was trying to resize an array as follows : if((editBufferCounter +
So i am trying to create something like a syncronized paint program by using

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.