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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:42:09+00:00 2026-06-10T02:42:09+00:00

I am learning about function pointers and thread in C. The following code will

  • 0

I am learning about function pointers and thread in C. The following code will create a thread that will read string from user. Then It will print the name which was entered by user on main thread. But I got Segmentation fault: 11

#include<pthread.h>
#include<stdio.h>

int agelimit;
char *string1, *string2;
void *getName();
//void (*getAge)();
main(){

    pthread_t thread1,thread2;

    string1 = malloc(1000);
    //scanf("%s",string0);
    pthread_create(&thread1, 0, getName, 0);


    //pthread_create(&thread2, 0, getAge, 0);
    sleep(10);
    printf("name is %s age is",string1);



}
void *getName(){

    int x;
    printf("enter name:");

    scanf("%s",&string1);
}
  • 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-10T02:42:11+00:00Added an answer on June 10, 2026 at 2:42 am

    Though your code needs some more improvement/error handling but it seems you need to change the

    scanf("%s",&string1);
    

    to

    scanf("%s",string1);
    

    to get it working now.

    Pl. refer the Link for a good tutorial on

    POSIX Threads Programming

    Explanation:

    string1 is a variable and stored in memory. So it is having a memory address say a1 (this is the starting address).
    Now what is stored in this memory address? This comes from your assignment statement below.

    string1 = malloc(1000);
    

    malloc ->allocates a chunk of 1000 bytes of memory and returns the starting address of the chunk, which is say p1.

    So now content of the variable string1 is p1 or in other words the memory cell a1 is now having p1(actually this should not be a single cell but a 4byte/8byte quantity..but I am assuming a single cell for simplicity).

    Now what scanf expects as its 2nd argument here?

    scanf("%s",&string1);
    
    A valid address where it can store the input which it has accepted.
    
    What is that in this example a1 or p1? - It is p1.
    

    So when you are giving &string1 as an argument to scanf, you were passing a1 to scanf ,which is not correct. It should get p1 to work properly, so you need to pass string1 and not &string1

    Hope this explanation helps. In general Pl. refer the c-faq link below, which is very helpful for understanding c concepts
    comp.lang.c Frequently Asked Questions

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

Sidebar

Related Questions

Learning about ARM NEON intrinsics, I was timing a function that I wrote to
I'm learning about classes and objects in c++ and tried the following code to
I am just now learning about function pointers and, as I was reading the
I learning to use Pointers. I have a few questions about an exercise code
I am learning about raw sockets. I heard that ping utility is using raw
I'm learning how to code in Java after after coming from C. In C
I am learning about jquery mobile. I came across a sample code which uses
I am learning about pointers and arrays in class. My trouble is in the
I am learning about pointers and the new operator in class. In my readArray
I am writing a program for a class assignment on learning about pointers and

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.