I'm new to C, I've already searched and I haven't found an answer, but I've been trying to get the program to give me a list with the name of the products typed in by the user followed by the sum of all prices and I've found the error:
clang-7 -pthread -lm -o main main.c
/tmp/main-7440c0.o: In function `main':
main.c:(.text 0x12b): undefined reference to 'N'
main.c:(.text 0x164): undefined reference to 'Digitanome'
main.c:(.text 0x17f): undefined reference to 'Lista'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
exit status 1
The code I've been trying is this one:
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
extern char (N[][40]);
int i;
char p;
int cod,cont, soma2;
char soma[100];
int Digitanome( char [][40], int );
void Lista( char [100][40], int );
typedef struct {
char produto[30];
char seçao [30];
float preco;
int cargo;
}Supermercado;
Supermercado compra;
int main(void)
{
char Nome[100][40] = { '\0' };
int qtdNomes = 0;
soma2 = 0;
do{
printf("\n\nEm qual seção está seu produto?");
printf("\n1-Frutas \n2-Doces \n3-Material de Limpeza\n --> ");
scanf("%d",&cod);
if(cod == 1){
*compra.seçao = *strcpy(compra.seçao,"Frutas");
}
if(cod == 2){
*compra.seçao = *strcpy(compra.seçao, "Doces");
}
if(cod == 3){
*compra.seçao = *strcpy(compra.seçao,"Material de Limpeza");
}
int Digitanome(char N[][40], int i);
{
printf("Informe o produto que você quer nesta seção: \n");
scanf("%s", & *N[i]);
*compra.produto = Digitanome( Nome, qtdNomes );
Lista( Nome, qtdNomes );
return i;
}
return 0;
printf("Informe o preço do produto: \n");
scanf("%f", &compra.preco);
soma2 = soma2 compra.preco;
printf("\nDeseja mais algum produto? \n4-Sim \n0-Não, sair \n --> ");
scanf("%d",&cont);
}while(cont == 4);
{
if (cont == 0)
printf("\nFIM DAS COMPRAS!\n");
void Lista(char p[100][40], int i);{
int j = 0;
for (; j < i; j )
printf("\nSeus produtos são:%s\n", compra.produto);
}
printf("Essa compra está custando: %i \n", soma2);
}
}
Can anyone explain to me what is happening and how to solve it?
CodePudding user response:
Several problems:
extern char (N[][40]);
You declare N
as extern
without initialization so you would also need to delcare it in another module with initialization. But you don't actually ever use the variable N
. You have N[][40]
as an argument to Digitanome
. Once you fix item numbers 2 and 3 below, you can remove extern char (N[][40]);
completely.
You define
Digitanome
andLista
insidemain()
. You need to define them outside ofmain()
.You have semicolons at the end of
Digitanome
andLista
function definitions. You need to remove those.You have code following the
return 0
statement.You call
Digitanome
from insideDigitanome
. That is probably not what you want.
Once you fix those problems, you will probably find more.