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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:38:07+00:00 2026-05-14T21:38:07+00:00

int num_arrays; char *p[20]; char tempc; int i=0; do { p[i]=malloc(sizeof(int)); scanf(%s,p[i]); tempc=*p[i]; ++i;

  • 0
       int num_arrays;
       char *p[20];
       char tempc;       
       int i=0;
       do
       { p[i]=malloc(sizeof(int));
        scanf("%s",p[i]);
        tempc=*p[i];
        ++i;
        }while(tempc=='x');
        num_arrays=atoi(p[0]);

When i write num_arrays=atoi(..),gcc give me segmentation fault or memory
stack is exceeded, I don’t understand why it behaves like that
can anyone explain, why?

  • 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-14T21:38:08+00:00Added an answer on May 14, 2026 at 9:38 pm

    You haven’t allocated any memory in p, so its elements point to random locations. You can allocate memory for strings with a maximum length of 100 like this:

    int i;
    for (i = 0; i < 20; i++)
      p[i] = malloc(101);
    

    Or else you want an array of characters, not of strings, in which case you should declare it like

    char p[20];
    

    In this case, you must not try to read strings into each element with scanf though.

    Update after the missing code part has been added:

    You are allocating sizeof(int) bytes of memory for your strings, which is most likely 4, i.e. your input strings read by scanf must not be longer than 3 characters. Otherwise you have a buffer overrun error, which may lead to the segmentation fault you are struggling with.

    Moreover, tempc=p[i] assigns a pointer value to a character variable! This value, converted to a character, will almost surely never be equal to 'x'.

    I guess you are trying to get the first character of p[i], which would be p[i][0] or *p[i].

    I also suspect that your loop condition is the opposite of what you had in mind: right now the loop repeats as long as tempc is equal to 'x' – you probably wanted to repeat until tempc becomes 'x'. And you should also check for your loop not running more than 20 times:

      ...
    }while(tempc != 'x' && i < 20);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My compiler, MPLAB C30 (GCC v3.23) is not compiling this code: if(font_info.flags & FONT_LOWERCASE_ONLY)
I'm reading in a line from a file (char by char, using fgetc()), where
I have a function for writing ppm files (a picture format) to disk. It
i'm trying to read contents of PNG file. As you may know, all data
Alright, be gentle, since I'm very much a novice to programming. So far I've
I have a function which determines if an array needs to be smoothed. I
This code receives a input file with 10 filenames, stores them into an 2d
guys I'm trying to compile my program in c but I'm getting this error
I'm trying to pass the array to the generate function and use its elements
I'm having a problem understanding how to read in a string to a structure

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.