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

If in C I write: int num; Before I assign anything to num ,
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
So as we all probably know, the atoi converts a char to a number.
If I have these code: unsigned char **keys; int *num_keys; int num_images = (int)
I have a function with following signature char requestApiCall(int num, const wchar_t* pParams =
Lets assume we have the following code: abstract class Base1 { protected int num;
I get an exception in my second print line. int num[] = {50,20,45,82,25,63}; System.out.print(Given
I have one class. Class First { private Second second; public First(int num, String
I have two tables: tickets ticket_id, int(11), NO, PRI, , auto_increment order_num, varchar(45), NO,

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.