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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:26:51+00:00 2026-05-28T03:26:51+00:00

I am stuck in how to fill a pointer array with strings using malloc.

  • 0

I am stuck in how to fill a pointer array with strings using malloc. In debug i see that when i fill the 1st pointer of array with a string, when its about to go to next pointer in the array it pass the next string in both 1st and second element… seems like when i use ptr[i]=buff; the ptr keeps showing in the buff array.

    #include<stdlib.h>
    #include<string.h>
    #define size 2       //array of 2 pointers
    int main()
{
    int i;
    char *ptr[size];
    char buff[80];

    for (i=0;i<size;i++)
    {
        memset(buff, 0, sizeof(char) * 80);
        printf("Enter name:\n");fflush(stdout);
        scanf("%s",buff);

        ptr[i]=(char*)malloc(strlen(buff));
        //ptr[i]=buff;                        //that was the mistake
        strncpy(ptr[i], buff, strlen(buff));    //->correct answer!
        printf("length %d\n",strlen(buff));
    }
    for (i=0;i<size;i++)
    {
        printf("prt[%d]=%s\n",i,ptr[i]);fflush(stdout);
    }
    for (i=0;i<size;i++)
    {
        free(ptr[i]);
    }
    return 0;
}

Another weird question that i have has to do with the length of the arrays in general. When an array is declared for example a[10] the pointer a points to the first element of the array. What i do not understand is where the length is being stored!? is it the previous memory address of the pointer a? Is it before? Or does it have to do with the compiler only?
Thanks. i hope that wasnt too much i asked. 🙂

  • 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-28T03:26:52+00:00Added an answer on May 28, 2026 at 3:26 am

    This:

    ptr[i]=buff;
    

    does not copy the string. It just copies a pointer. So not have you caused a memory leak (you have no way to access the memory you just allocated), but it messes your program up, because ptr[i] now points at buff, so every time you read a new string, it will appear to affect all elements of ptr[].

    Do this instead:

    strncpy(ptr[i], buff, BUF_SIZE);
    

    Note also that it’s considered bad practice to use gets; consider what would happen if the user were to type a string with more than 9 characters.

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

Sidebar

Related Questions

Being stuck with a legacy database schema that no longer reflects your data model
I'm stuck with this one! When I alert the object that I stringify the
Im trying to create a new master page without using a table and its
I want to create a custom form that a user can fill out for
Hey all I am trying to fill a html form using a java program
I am trying to call a function that takes char** as a parameter. Its
I have a form that people must fill in. There has to be checks
I'm stuck. I'm tring to fill a listview with fields of a SQLite table.
Really stuck on trying to write code to unzip a file or directory on
Kinda stuck here... I have an application with lets say 5000 rows of data

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.