이 코드는 Windows에서 Codeblocks로 제대로 컴파일되며, "Aggiungi"(삽입) 함수를 실행하려면 1을 누르면 Name, Surname 및 성을 입력 한 후 Enter 키를 누르면 (전화를 삽입해야 할 때) 충돌합니다.C 문자 포인터 유형의 구조가 함수에 값을 삽입하지 못했습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
typedef struct rubrica
{
char *nome;
char *cognome;
char *tel;
} rubrica;
void aggiungi(rubrica *contatto, int ncont);
int main(void)
{
rubrica *contatto[MAX];
int act, ncont=0;
do
{
printf("1)AGGIUNGI\n");
printf("2)VISUALIZZA\n");
printf("0)ESCI\n");
scanf("%d", &act);
if(act==1)
{
aggiungi(*contatto, ncont);
ncont++;
}
} while (act!=0);
return 0;
}
void aggiungi(rubrica *contatto, int ncont)
{
printf("\n________________\n");
contatto[ncont].nome=malloc(21*sizeof(char*));
printf("\nNome: ");
scanf("%s", contatto[ncont].nome);
contatto[ncont].cognome=malloc(21*sizeof(char*));
printf("\nCognome: ");
scanf("%s", contatto[ncont].cognome);
contatto[ncont].tel=malloc(12*sizeof(char*));
printf("\nTelefono: ");
scanf("%s", contatto[ncont].tel);
}
에 오신 것을 환영에
및
에
변경하려고 할 수 있습니다! 디버거를 사용하여 코드를 단계별로 실행하는 방법을 배워야 할 필요가있는 것 같습니다. 좋은 디버거를 사용하면 한 줄씩 프로그램을 실행하고 예상 한 곳에서 벗어난 곳을 볼 수 있습니다. 프로그래밍을 할 때 필수적인 도구입니다. 추가 읽기 : [작은 프로그램을 디버깅하는 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). –
'rubrica * contatto [MAX];'->'rubrica contatto [MAX]'', 다음''aggiungi (* contatto, ncont) '->'aggiungi (contatto, ncont);' – BLUEPIXY
Major :'rubrica * contatto [MAX];'는 포인터 요소에 할당 된 메모리가 없습니다. Minor :'malloc (21 * sizeof (char *))'는'malloc (21 * sizeof (char))'이어야합니다. –