Программа на C обнаружила ошибку в хранилище строки, несмотря на предварительное присвоение ей массива - PullRequest
0 голосов
/ 02 октября 2018

Рассмотрим приведенный ниже код:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MAX 30
int x;
int p;
char name[50];//to store name of employee inserted from user
char desig[50];//to store designation of employee
int sal;//function paramater for salary
int salary;//tp store salary of employee
int expx;//to store experience
int experience;//to store experience
char dep[50];//to store department name
char department[50];//to store department name
int promotion[100];
int tester[100];//stack to store employee numbers (eno) of employees who designation is tester
int coder[100]; //stack to store employee numbers(eno) of employees who designation is coder
int researcher[100]; //stack to store employee numbers(eno) of employees who designation is researcher
int topT=-1;//top variable for tester stack
int topC=-1;//top variable for coder stack
int topR=-1;//top variable for researcher stack

/*Structure to define charecteristics and content of linked list node*/
struct EMP
{
int empno;
char empName[MAX];
char designation[MAX];
char department[MAX];
int experience;
int salary;
struct EMP *next;
};
/*------------------------------------*/





/*------------------Function to insert a new node with employee details into linked list--------------------------------*/
struct EMP* InsertEmployeeRecords(struct EMP *front, int id, char name[], char desg[] ,char dep[],int expx,int sal)
{
struct EMP *newnode;
newnode = (struct EMP*) malloc(sizeof(struct EMP));
if (newnode == NULL)
{
printf("\nAllocation failed\n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
strcpy(newnode->department,dep);
newnode->salary=sal;
newnode->experience=expx;
newnode->next = front;
front = newnode;
return(front);
}
/*-----------------------------------------------------------------------*/








/*---------Function to print Linked list node sent to it--------------------*/
void PrintEmployeeRecords(struct EMP *p)
{
printf("\n---------------------------------------------------------------");
printf("\nEmployee Details...");
printf("\nEmp No : %d", p->empno);
printf("\nName : %s", p->empName);
printf("Designation : %s", p->designation);
printf("Department : %s", p->department);
printf("Salary : %d",p->salary);
printf("\nExperience :%d",p->experience);

printf("\n-------------------------------------\n");
}
/*-----------------------------------------------------------------------*/







/*--------------Function to delete emplyee records---------*/
struct EMP* DeleteEmployeeRecords(struct EMP *front, int id)
{
struct EMP *ptr;
struct EMP *bptr; /* bptr is pointing to the node behind ptr */

if (front->empno == id)
{
ptr = front;
printf("\nNode deleted:");
PrintEmployeeRecords(front);
front = front->next;
free(ptr);
return(front);
}

for(ptr=front->next, bptr=front; ptr!=NULL; ptr=ptr->next, bptr=bptr->next)
{
if (ptr->empno == id)
{
printf("\nNode deleted:");
PrintEmployeeRecords(ptr);
bptr->next = ptr->next;
free(ptr);
return(front);
}
}
printf("\nEmployee Number %d not found ", id);
return(front);
}

/*------------------------------------------------------------------------------------*/





/*----------------------Function to SearchEmployeeRecords-------------*/
void SearchEmployeeRecords(struct EMP *front, int key)
{
struct EMP *ptr;

for (ptr = front; ptr != NULL; ptr = ptr -> next)
{
if (ptr->empno == key)
{
printf("\nKey found:");
PrintEmployeeRecords(ptr);
return;
}
}
printf("\nEmployee Number %d not found ", key);
}
/*---------------------------------------------------------------------------*/






/*-----------------------------------------------------------------------------------------*/
void DisplayEmployeeRecords(struct EMP *front)
{
struct EMP *ptr;

for (ptr = front; ptr != NULL; ptr = ptr->next)
{
PrintEmployeeRecords(ptr);
}
}
/*---------------------------------------------------------------------------------------*/




/*----------------------------------------------------------------------------------------*/
void menu()
{
system("cls");
printf(" \n  :::::::::::::::  |EMPLOYEES RECORD MANAGEMENT|  :::::::::::::::::::::::::: \n");

        printf("\t\t\t\t1> Add Employee's Records\n"); /// option for add record

    printf("\t\t\t\t2> Delete Employee's Records\n"); /// option for showing existing record

    printf("\t\t\t\t3> Display Employee's Records\n"); /// option for editing record

    printf("\t\t\t\t4> Search Employee's Records\n"); /// option for deleting record

    printf("\t\t\t\t5> Exit System\n"); /// exit from the program


printf("------------------------------------------------------------------------------\n");
}
/*-------------------------------------------------------------------------------------*/






/*-------------------------------------------------------------------------------------*/
char option()
{
char choice;
printf("\n\nEnter your operation choice: ");
switch(choice=getche())
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6': return(choice);
default : printf("\nInvalid choice.");
}
return choice;
}
/*--------------------------------------------------------------------------------------*/






/* -------------------------------------------------------------------------------------*/
int main()
{
struct EMP *linkList;
char name[21], desig[51];
char choice;
int eno;char line_buffer[50],line_buffer1[50],line_buffer2[50];

linkList = NULL;

printf("\nWelcome to Employee Management System\n");

 /*Function call */

do
{
    menu();
choice = option(); /*to choose oeration to be performed */

switch(choice)
{
case '1':
    printf("\n----------------------------------------------------");
    printf("\nEnter the Employee Number : ");
    // put input into line buffer
    fgets(line_buffer, sizeof(line_buffer), stdin);
    // use sscanf on your line_buffer instead of stdin
    sscanf(line_buffer, "%d", &eno);

    printf("Enter the Employee name : ");
    fgets(name, sizeof(name), stdin);

    printf("Enter the Employee Designation\n1:Coder\t2:Tester\t 3:Researcher : ");

    {
    scanf("%d",&p);
    switch(p)
    {
    case 1:
    ++topC;
    coder[topC]=eno;
    desig[]="coder";//showing syntax error in this line
    break;

    case 2:
    ++topT;
    tester[topT]=eno;
    desig[]="tester";//showing syntax error in this line
    break;

    case 3:
    ++topR;
    researcher[topR]=eno;
    desig[]="researcher";**//showing syntax error in this line**
    break;
    }}
    printf("Enter the Employee department : ");
    fgets(dep, sizeof(name), stdin);

    printf("Enter the Employee experience(in years) : ");
    // put input into line buffer
    fgets(line_buffer1, sizeof(line_buffer1), stdin);
    // use sscanf on your line_buffer instead of stdin
    sscanf(line_buffer1, "%d", &expx);

    printf("Enter the Employee salary : ");
    // put input into line buffer
    fgets(line_buffer2, sizeof(line_buffer2), stdin);
    // use sscanf on your line_buffer instead of stdin
    sscanf(line_buffer2, "%d", &sal);
    printf("\n----------------------------------------------------\n");

    linkList = InsertEmployeeRecords(linkList, eno, name, desig , dep , expx , sal);
    break;

case '2':

printf("\n\nEnter the employee number to be deleted: ");
scanf("%d", &eno);
linkList = DeleteEmployeeRecords(linkList, eno);
printf("enter 0 to continue");
scanf("\n%d",&x);
break;

case '3':

if (linkList == NULL)
{
printf("\nList empty.");
break;
}
DisplayEmployeeRecords(linkList);
printf("enter 0 to continue");
scanf("\n%d",&x);
break;

case '4':

printf("\n\nEnter the employee number to be searched: ");
scanf("%d", &eno);
SearchEmployeeRecords(linkList, eno);
printf("enter 0 to continue");
scanf("\n%d",&x);
break;

case '5':break;
}
} while (choice != '5');
return 0;
}

Это сегмент программы, в котором я получаю синтаксическую ошибку:

printf("Enter the Employee Designation\n1:Coder\t2:Tester\t 3:Researcher : ");

    {
    scanf("%d",&p);
    switch(p)
    {
    case 1:
    ++topC;
    coder[topC]=eno;
    desig[]="coder";//showing syntax error in this line
    break;

    case 2:
    ++topT;
    tester[topT]=eno;
    desig[]="tester";//showing syntax error in this line
    break;

    case 3:
    ++topR;
    researcher[topR]=eno;
    desig[]="researcher";**//showing syntax error in this line**
    break;
    }}

Почему это происходит?

Я просто присваиваю строковое значение массиву символов, который был объявлен в начале.

Я правильно объявил массив символов в начале файла, поэтому область действия определенной переменной действительна в течение всейПрограмма.

Тем не менее, я получаю синтаксическую ошибку.

1 Ответ

0 голосов
/ 02 октября 2018

Вы не можете просто назначить строки, такие как

char foo[50];
foo = "foobar";

Вы должны скопировать данные:

#include <string.h>

// ...

char foo[50];
strcpy(foo, "foobar");
...