[英]Why does my C program output the memory address instead of the value?
[英]Changing the memory address instead of the value in a struct on C
这是我的程序。 (对不起,结构和变量的常量和名称是我的语言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
/*CONSTANTES*/
#define MAX_CHAR 8
#define MAX_NOM_IMPR (20+1)
#define NUM_ELEM 10
#define MAX_IMPR 4
#define MAX_FICHEROS 6
#define ERROR_ARGC 0
#define ERROR_LONG 1
#define ERROR_COLALLENA 2
#define ERROR_NO_OPCION 3
#define ERROR_MAX_IMPR 4
#define ERROR_NOM_REP 5
#define ERROR_NO_IMP 6
#define ERROR_NO_IMP_REP 7
#define ERROR_LONG_IMPR 8
/*TIPOS DE DATOS*/
typedef char Telemento[MAX_CHAR+5];
typedef struct{
Telemento arraycola[NUM_ELEM];
int inicio;
int final;
}TCola;
typedef char TNombreImpresora[MAX_NOM_IMPR];
typedef struct{
TNombreImpresora nombreimpresora;
int numerodeficherosencola;
TCola colaImpresora;
}TImpresora;
typedef struct{
TImpresora impresora;
int ocupado;
}TCelda;
typedef TCelda Tlistaimpresora[MAX_IMPR];
int main(){
Tlistaimpresora listaimpresora;
listaimpresora[1].ocupado=1;
printf("%s", listaimpresora[1].ocupado);
}
当我运行它时,它崩溃了,我发现:不改变ocupado
的值, ocupado
改变了内存地址,我也不知道为什么。 我究竟做错了什么?
您没有发布Tlistaimpresora
结构详细信息。
但是,存在一些常见的错误。
您已经定义了一个类型为Tlistaimpresora
变量,并且正在访问listaimpresora[1]
?
考虑到Tlistaimpresora
是struct abc *
的typedef,您没有为变量分配内存。
编辑:
好吧,在完整的代码之后,看来您的问题在这里
printf("%s", listaimpresora[1].ocupado);
listaimpresora[1].ocupado
是int
类型,需要%d
格式说明符。 %s
说明符用于打印字符串,并且希望为其打印以null终止的字符串。
您正在以字符串格式打印整数变量。 因此,这就是导致分割错误的原因。 所以把它变成
printf("%d", listaimpresora[1].ocupado);
您将得到答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.