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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:33:10+00:00 2026-06-12T16:33:10+00:00

My code does not work. I get run time error at the moment i

  • 0

My code does not work. I get run time error at the moment i accept a string. What is the problem with this code?

//this is what i have in main()
char *ele,*s[max];
int *count,temp=0;
count=&temp;   
printf("Enter string to insert: ");
scanf("%s",ele);
addleft(s,ele,count);

//following is the function definition
void addleft(char *s[max],char *ele,int *count)
{
    int i;
    if((*count)==max)
    {
        printf("Queue full!\n");
        return;
    }
    for(i=*count;i>0;i--)
        strcpy(s[i],s[i-1]);
    strcpy(s[0],ele);
    (*count)++;
    printf("String inserted at left!\n");
}
  • 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-06-12T16:33:11+00:00Added an answer on June 12, 2026 at 4:33 pm

    ele is an uninitialised char* and has no memory associated with it and scanf() will be attempting to write to it causing undefined behaviour, a segmentation fault is probable.

    You need to either dynamically allocate memory for ele or declare a local array and prevent buffer overrun when using scanf():

    char ele[1024];
    if (1 == scanf("%1023s", ele))
    {
        /* Process 'ele'. */
    }
    

    Additionally, the function addleft() is using strcpy() on s, which is an array of char* and each of the char* in the array is unitialised. This is undefined behaviour and a probable segmentation fault. To correct, you could use strdup() if it is available otherwise malloc() and strcpy():

    /* Instead of:
           strcpy(s[0],ele);
       use:
     */
    s[0] = strdup(ele);
    

    Note that the for loop inside the addleft() function is dangerous as the char* contained within s are not necessarily of the same length. This could easily lead to writing beyond the end of arrays. However, as the elements are addresses of dynamically allocated char* you can just swap the elements instead of copying their content.

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

Sidebar

Related Questions

I have following code that does not work: I never get to goToFoodDetail .
I dont get why this simple line of code does not work: <?php $someVariable
This javascript code does not work in IE8, but works in Firefox and Google
I have the feedparser.py in my project directory but my code does not work
How can I do this? (The following code does NOT work, but I hope
I'm newbie in Python. I can't understand why this code does not work: reOptions
I have following code that does not work due to a being a value
If I use strict mode the following code does not work. It fails on
I simply could not understand why the following code does not work. What could
The code below does not work but meant to illustrate what I want to

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.