Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中的字符串数组(null)_C_Arrays_String_Null - Fatal编程技术网

c语言中的字符串数组(null)

c语言中的字符串数组(null),c,arrays,string,null,C,Arrays,String,Null,嗯,我必须制作一个程序,要求用户输入名称,然后将其插入数组,然后打印数组,但它只打印null,我不知道我做错了什么,代码是西班牙语的,但函数“insertar”只插入新名称,listar必须列出数组中的项 #include<stdio.h> #define MAX 5 #include<string.h> char arreglo[MAX]; int indice; //prototipo de funcion int menu (char texto[],int

嗯,我必须制作一个程序,要求用户输入名称,然后将其插入数组,然后打印数组,但它只打印null,我不知道我做错了什么,代码是西班牙语的,但函数“insertar”只插入新名称,listar必须列出数组中的项

#include<stdio.h>
#define MAX 5
#include<string.h> 

char arreglo[MAX];
int indice;


//prototipo de funcion
int menu (char texto[],int n);
void insertar (char dato);
void listar();

main(){
//variable locales
    int opcion;
    char dato[50];
    indice = -1;

    do{
        opcion = menu("\n1) Insertar\n2) Borrar\n3) Actualizar\n4) Ordenar\n5) listar\n6) Buscar\n7) Salir\n",7);

        switch(opcion) 
        {
            case 1://insertar 
                if(indice < MAX-1){
                    printf("Dame el dato a insertar");
                    scanf("%s", dato);
                    insertar(dato[50]);
                }//fin if
                else{
                     printf("error no hay espacio" );
                }//fin else
            break;

            case 2:
                 printf("\nBorrar");
            break;

            case 3:
                 printf("\nActualizar");
            break;

            case 4:
                 printf("\nordenar");
            break;

            case 5://listar
                 printf("\nListando\n");
                 listar();
                 printf("%s \t ", arreglo);

            break;

            case 6:
                 printf("\nBuscar");
            break;

            case 7:
                 printf("\nHasta luego...");
            break;
        }//fin switch

    }while(opcion  != 7);
}//finn del main

int menu (char texto[], int n){
   int opcion;
   do{
        printf("%s", texto);
        scanf("%d",&opcion);
        if(opcion <1 || opcion >n){
        printf("error: opcion no valida");
        }//fin del if
   }while(opcion <1 || opcion >n);
return opcion;
}//fin funcion

void insertar (char dato){
    indice ++;

    arreglo[indice]= dato;
}//fin funcion

void listar(){
    char i;
    for(i=0; i<=indice ; i++){
        printf( "%s \t ", arreglo[i]);



    }
    printf("\n");
}//fin funcion
#包括
#定义最大值5
#包括
char arreglo[MAX];
int-indice;
//功能协议
int菜单(char texto[],int n);
无效插入器(字符dato);
void listar();
main(){
//可变区域设置
国际组织;
char-dato[50];
指数=-1;
做{
opcion=menu(“\n1)Insertar\n2)Borrar\n3)Actualizar\n4)Ordenar\n5)listar\n6)Buscar\n7)Salir\n”,7);
交换机(opcion)
{
案例1://插入者
如果(指示<最大值-1){
printf(“插入者达托夫人”);
scanf(“%s”,dato);
插入器(dato[50]);
}//fin if
否则{
printf(“错误号hay espacio”);
}//其他
打破
案例2:
printf(“\n伯拉”);
打破
案例3:
printf(“\n虚拟化程序”);
打破
案例4:
printf(“\nordenar”);
打破
案例5://listar
printf(“\nListando\n”);
listar();
printf(“%s\t”,arreglo);
打破
案例6:
printf(“\n USCAR”);
打破
案例7:
printf(“\nSta luego…”);
打破
}//翅片开关
}而(opcion!=7);
}//芬德梅因酒店
int菜单(char texto[],int n){
国际组织;
做{
printf(“%s”,texto);
scanf(“%d”和&opcion);
如果(opcion){
printf(“错误:opcion no valida”);
}//菲德尔伊夫酒店
}while(opcion);
返回选项;
}//鳍函数
无效插入器(字符dato){
indice++;
阿瑞格洛[印度]=达托;
}//鳍函数
void listar(){
char i;

对于(i=0;i创建字符串数组的一种方法是使用二维字符数组

#define MAX 5
...
char arreglo[MAX];
...
char dato[50];
你会的

#define MAX 5
#define MAX_LEN 50
...
char arreglo[MAX][MAX_LEN];
...
char dato[MAX_LEN];
下一步是将
scanf
更改为
scanf(“%49s”,dato);
,以防止用户溢出缓冲区

insertar(dato[50]);
应该是
insertar(dato);
dato
数组传递给函数

最后,需要使用
strcpy
函数将字符串复制到数组中

void insertar(char dato[]) {
    indice++;
    strcpy(arreglo[indice], dato);
}
插入器方法声明应为:

void insertar(char dato[]);

制作字符串数组的一种方法是使用二维字符数组

#define MAX 5
...
char arreglo[MAX];
...
char dato[50];
你会的

#define MAX 5
#define MAX_LEN 50
...
char arreglo[MAX][MAX_LEN];
...
char dato[MAX_LEN];
下一步是将
scanf
更改为
scanf(“%49s”,dato);
,以防止用户溢出缓冲区

insertar(dato[50]);
应该是
insertar(dato);
dato
数组传递给函数

最后,需要使用
strcpy
函数将字符串复制到数组中

void insertar(char dato[]) {
    indice++;
    strcpy(arreglo[indice], dato);
}
插入器方法声明应为:

void insertar(char dato[]);