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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:38:54+00:00 2026-05-12T17:38:54+00:00

char **arr; arr = (char **)calloc(1,sizeof(char*)); for(i = 0; i< 16; i++) if(arr[i] =

  • 0
 char **arr;
 arr = (char **)calloc(1,sizeof(char*));

 for(i = 0; i< 16; i++)
    if(arr[i] = (char *)calloc(1, 2*sizeof(char)) == NULL)
        perror("Memory cannot be allocated to arr[i]", %d);

The above code throws an error inside the for loop, when i am trying to allocate memory to arr[i]. Is anything wrong with this allocation. Essentially, i want to store 16 strings of length 2. I’ve tried it with array of pointers too (char *arr[16]). I have tried looking for resources on double pointer initializations using malloc() and calloc() and couldn’t find many . If you could point out some links, that would be greatly appreciated.
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-12T17:38:54+00:00Added an answer on May 12, 2026 at 5:38 pm

    You need to allocate enough memory for 16 pointers, not just one.

    arr = (char **)calloc(16, sizeof(char*));
    

    What happens with your code is that arr has enough memory only for one pointer, so arr[0] = <something> is correct, but arr[1] and higher is touching memory that doesn’t belong to the program.

    Additionally, the way you assign the string pointers is wrong. You are assigning 0 or 1 values, depending on whether the result if calloc is NULL. You need to add parentheses there:

    if ((arr[i] = (char *)calloc(1, 2*sizeof(char))) == NULL)
        perror("Memory cannot be allocated to arr[%d]", i);
    

    Er even better:

    for(i = 0; i < 16; i++) {
        arr[i] = (char *)calloc(1, 2*sizeof(char));
        if (arr[i] == NULL) {
            perror("Memory cannot be allocated to arr[%d]", i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the 'sizeof' array: char* arr[] = { abc, def };
What exactly happens, in terms of memory, when i declare something like: char arr[4];
I have the following code : int *edges[500]; char arr[] = {'c','d'}; edges[0] =
I have this program #include <stdio.h> int main() { char arr[100]; printf(%d, (int)sizeof(0,arr)); }
When I run this code: int arr[3] = {2,3,4}; char *p; p = (char*)arr;
char arr[100]; cin.get(arr,100); Is this safe? Will the null-character be appended at the end
char label[8] = abcdefgh; char arr[7] = abcdefg; printf(%s\n,label); printf(%s,arr); ====output========== abcdefgh abcdefgÅ Why
Are these two constructs equivalent? char[] arr = new char[5]; for (char x :
char myData[505][3][50]; //2D array, each 50 chars long char **tableData[505] = {NULL}; const char*
I executed this code after compiling in codeblocks:- #include <stdio.h> int main() { char

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.