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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:08:39+00:00 2026-05-27T10:08:39+00:00

This code is about ‘struct’ in C.. I created a struct spieler with the

  • 0

This code is about ‘struct’ in C..

I created a struct spieler with the properties name and age..
By using the for-loop I let the user to create the struct objects.
they are named as sp[i] –> sp1, sp2 etc.

the problem is the objects are created. But I can use them only inside the for-loop.
If I want to get the value of “sp1.name” in main function, it doesn’t work.
How can I solve it?

struct spieler{
  char name[20];
  int age;
};

void erzeuge();

int main() {

int anzahl = 2;
printf("Anzahl Spielern: ");
scanf("%d",&anzahl);

erzeuge(anzahl);

printf("Es sind %d Spielern",anzahl);
/*for(i;i<anzahl;i++){
    printf("%d.%s",i, sp[i].name);
}*/

getchar();

}

void erzeuge(int anzahl){

int i=0;
for(i;i<anzahl;i++){
    struct spieler sp[i];
    printf("Struct fuer Spieler_%d wurde erzeugt\n", i);
    getchar();
    printf("Name: ");
    scanf("%s",sp[i].name);

    printf("%s\n",sp[i].name);
}

  • 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-27T10:08:40+00:00Added an answer on May 27, 2026 at 10:08 am

    You should declare sp as a pointer at the global scope, and allocate memory for it inside the function erzeuge using malloc.

    #include <stdlib.h>
    #include <stdio.h>
    
    struct spieler {
        char name[20];
        int age;
    };
    
    struct spieler *sp;    // Add this
    
    void erzeuge();
    
    int main() {
        int anzahl;
        printf("Anzahl Spielern: ");
        scanf("%d", &anzahl);
    
        erzeuge(anzahl);
    
        printf("Es sind %d Spielern\n", anzahl);
    
        int i;
        for(i = 0; i < anzahl; i++){
            printf("%d.%s\n", i, sp[i].name);
        }
    
        if (sp) {
            free(sp);
        }
        getchar();
        return 0;
    }
    
    void erzeuge(int anzahl) {
        // Add the following line to allocate memory
        sp = (struct spieler*) malloc(anzahl * sizeof(struct spieler));
    
        int i;
        for (i = 0; i < anzahl; i++) {
            // Remove the following line because it create an array of "i" elements
            // struct spieler sp[i];
            printf("Struct fuer Spieler_%d wurde erzeugt\n", i);
            getchar();
            printf("Name: ");
            scanf("%s",sp[i].name);
    
            printf("%s\n",sp[i].name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this code, About half-way through the concatenation loop, $xml becomes null
There are a lot of posts on here about this, I'm using this code
Let's say we have this code (forget about prototypes for a moment): function A(){
this is my code about bubble sort. #include stdafx.h #include <iostream> #include <cstdlib> using
The compiler complains about this code: HashMap<String,int> userName2ind = new HashMap<String,int>(); for (int i=0;
There's something very unsatisfactory about this code: /* Given a command string in which
I'm VERY confused as to why this code Html.ActionLink(About, About, Home, new { hidefocus
according to this code i dont understand about NSClassFromString how did i know viewControllerName
Right, this code goes through a rather large multidimensional array (has about 28,000 rows
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code

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.