2016-12-22 2 views
0

이 코드는 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); 


} 
+0

에 오신 것을 환영에

rubrica contatto[MAX]; 

aggiungi(*contatto, ncont); 

rubrica *contatto[MAX]; 

변경하려고 할 수 있습니다! 디버거를 사용하여 코드를 단계별로 실행하는 방법을 배워야 할 필요가있는 것 같습니다. 좋은 디버거를 사용하면 한 줄씩 프로그램을 실행하고 예상 한 곳에서 벗어난 곳을 볼 수 있습니다. 프로그래밍을 할 때 필수적인 도구입니다. 추가 읽기 : [작은 프로그램을 디버깅하는 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). –

+2

'rubrica * contatto [MAX];'->'rubrica contatto [MAX]'', 다음''aggiungi (* contatto, ncont) '->'aggiungi (contatto, ncont);' – BLUEPIXY

+2

Major :'rubrica * contatto [MAX];'는 포인터 요소에 할당 된 메모리가 없습니다. Minor :'malloc (21 * sizeof (char *))'는'malloc (21 * sizeof (char))'이어야합니다. –

답변

0

내가 선언에서 보듯이 (rubrica *contatto[MAX];) contatto 포인터의 배열입니다,하지만 코드는 배열이나 구조의 단지 배열 (가능성)에 포인터를 필요가 있다는 생각을 리드.

내 제안은 데이터 선언을 다시 디자인하는 것입니다. 다음과 같이

예를 들어, 시도 :

#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]; // array of items 
    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); // contatto is used without * 
      ncont++; 
     } 
    } while (act != 0); 
    return 0; 
} 

void aggiungi(rubrica *contatto, int ncont) // here *contatto is instead of contatto [] 
{ 
    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); 
} 
+0

나는 먼저 aggiungi (& contatto, ncont)에서 함수 aggiungi에 전달 된 인수를 변경했다. 그러나 그것은 효과가 있었지만 경고와주의를 기울였다. 당신의 방식대로 잘 작동합니다. 하지만 난 간단한 배열을 만들 때 일어나는 일을 이해하지 못한다. 그리고 그것을 인수로 함수에 전달하지만, 함수는 첫 번째 인수를 포인터로 갖는다. –

+0

배열은 함수에 전달 될 때 포인터로 쇠퇴합니다. –

+0

그래서 함수는 배열의 포인터에서 작동합니까? –

1

당신은 rubrica에 대한 메모리를 할당하지 않습니다, 당신 만의 포인터에 대한 메모리를 할당 할 수 있습니다. 이로 인해 잘못된 쓰기 메모리 액세스가 발생합니다. 당신은 스택 오버플로

aggiungi(contatto, ncont); 
+0

이미이 방법으로 시도했지만 컴파일되지 않습니다 ... –

+0

내 실수는 함수 인수도 변경했습니다! –