Home > Back-end >  strcpy does not copy the static string to the allocated space
strcpy does not copy the static string to the allocated space

Time:08-31

I tried to generate a little list for studying. But now I get an error message:

feste_liste.c:25:25: error: a label can only be part of a statement and a declaration is not a statement case 0: char letter1[] ="Whisky\n";strcpy(zeiger->Buchstabe,letter1); break;

this error occurs also for the other 3 cases.

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

struct fest {
    struct fest* next;
    char Name[10];
    int  Zahl;
};

struct fest* anker = NULL;

int main (void){
   
    for(int i=0; i< 4; i  )
    {
        struct fest* zeiger;
        if((zeiger=malloc(sizeof(struct fest)))!=NULL)
        {
            zeiger->Zahl = i 1;
            
            switch(i){
                case 0: char letter1[] ="Whisky\n";strcpy(zeiger->Name,letter1); break; //error
                case 1: char letter2[] ="Tango\n";strcpy(zeiger->Name,letter2); break;
                case 2: char letter3[]="Foxtrott\n";strcpy(zeiger->Name,letter3); break;
                case 3: char letter4[]="X-Ray\n";strcpy(zeiger->Name,letter4); break;
            }

            zeiger->next = anker;
            anker = zeiger;
        }
    }
    struct fest* abruf= anker;

    while(abruf !=NULL){
        printf("%d %s\n", abruf->Zahl, abruf->Name);
        abruf = abruf->next;
    }

    return 0;
}´´´

CodePudding user response:

You need to put the declaration inside a statement block, not directly after the case label.

            switch(i){
                case 0: {char letter1[] ="Whisky\n";strcpy(zeiger->Name,letter1); break; }
                case 1: {char letter2[] ="Tango\n";strcpy(zeiger->Name,letter2); break;}
                case 2: {char letter3[]="Foxtrott\n";strcpy(zeiger->Name,letter3); break;}
                case 3: {char letter4[]="X-Ray\n";strcpy(zeiger->Name,letter4); break;}
            }

But there's no need to declare those arrays at all, you can just copy from the literals.

strcpy(zeiger->Name, "X-Ray\n");

CodePudding user response:

Each case takes an expression as its execution scope, you can (in this case need to) define your own execution scope by encapsulating multiple expressions in curly-braces:

switch(i){
            case 0: {char letter1[] ="Whisky\n";strcpy(zeiger->Name,letter1); break;} 
            case 1: {char letter2[] ="Tango\n";strcpy(zeiger->Name,letter2); break;}
            case 2: {char letter3[]="Foxtrott\n";strcpy(zeiger->Name,letter3); break;}
            case 3: {char letter4[]="X-Ray\n";strcpy(zeiger->Name,letter4); break;}
        }

Or in a more readable way:

switch(i){
            case 0: {
                char letter1[] ="Whisky\n";
                strcpy(zeiger->Name,letter1);
                break;
            } 
            case 1: {
                char letter2[] ="Tango\n";
                strcpy(zeiger->Name,letter2);
                break;
            }
            case 2: {
                char letter3[]="Foxtrott\n";
                strcpy(zeiger->Name,letter3);
                break;
            }
            case 3: {
                char letter4[]="X-Ray\n";
                strcpy(zeiger->Name,letter4);
                break;
            }
        }
  •  Tags:  
  • c
  • Related