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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:19:09+00:00 2026-06-14T01:19:09+00:00

I have this example file example.c #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct

  • 0

I have this example file

example.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct {
    char *name;
} player;

void
playercreation(player *p, int nr)
{
    p=malloc(sizeof(player));
    char stringtemp[10];
    printf("Create player %d:\nWrite your name (max 10 letters): ", nr);
    scanf("%s", stringtemp);
    p->name=malloc(sizeof(*(p->name))*11);
    strcpy(p->name, stringtemp);
    p->drawnlines=0;
    p->squares=0;
}

void
playercreationMenu(player *p1, player *p2)
{
    playercreation(p1, 1);
    playercreation(p2, 1);
}

void
confirmPlayer(player *p)
{
    printf("player %s created\n", p->name);
}

int
main(void)
{
    player p1, p2;
    playercreationMenu(&p1, &p2);
    confirmPlayer(&p1);
    confirmPlayer(&p2);
}

In my real program this gives me a segmentation fault as im trying to access something in the player structure that does not exist as the player never is created, but in this example its shown by the fact that the players name is (null) although the name was given in the playercreationMenu function. Why is this?

  • 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-14T01:19:11+00:00Added an answer on June 14, 2026 at 1:19 am

    You write

    player p1, p2;
    playercreationMenu(&p1, &p2);
    

    But in the constructor function, you have

    void playercreation(player *p, int nr)
    {
        p = malloc(sizeof(player));
    
        ...
    }
    

    So you’re assigning a malloc()ated block of memory to the local variable containing the address of an already-allocated (on the stack!) structure… Drop the call to malloc() and you’ll be fine.

    You’re also using the count variable before initializing it or assigning a value to it:

    int count;
    ...
    p->name = malloc(sizeof(char) * (count + 1));
    

    So one, this is undefined behavior, two, you’ll get a quite random amount of memory (which makes it possible to write past its bounds -> another undefined behavior), or a NULL pointer if this would result in too much memory allocated (a third undefined behavior).

    Three UBs in one line – do expect it to crash.

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

Sidebar

Related Questions

In one file I have this: #include <stdio.h> #include <stdlib.h> static struct node* mynode;
I have this code for example. #include <stdlib.h> #include <stdio.h> #define array_size 3 typedef
OK. For example I have this line in my txt file: 1|1,12;7,19;6,4;8,19;2,2 As you
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
I have for example .pdf file (path to that file). How to open this
I have one file (for example: test.txt), this file contains some lines and for
I have a file containing some data (for example, 00927E2B112DB958......). This data is a
So I found this great C FFMpeg official example which I simplified: #include <stdlib.h>
I am using this code: #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include
I have this php www.example.com/template.php and would like to include it in a index.php

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.