#c #structure #unions
#c #структура #объединения
Вопрос:
Вот следующая проблема, я получаю подтверждение из несовместимых типов указателей в следующем коде:
ЧТОБЫ возобновить это для вас, строка 43 выглядит следующим образом:
aux->sig=nodo;
Строка 60 :
aux2=aux2->sig;
По сути, я получаю каждую ошибку в «aux2»
Что является причиной этого? И если бы вы могли мне это объяснить, я студент и я в основном новичок в программировании.
proyecto2mod.c: In function ‘agrega_nombre’:
proyecto2mod.c:43: warning: assignment from incompatible pointer type
proyecto2mod.c:60: warning: assignment from incompatible pointer type
proyecto2mod.c: In function ‘busca_cliente’:
proyecto2mod.c:105: warning: assignment from incompatible pointer type
proyecto2mod.c: In function ‘ordena’:
proyecto2mod.c:118: warning: assignment from incompatible pointer type
proyecto2mod.c:119: error: request for member ‘nombre’ in something not a structure or union
proyecto2mod.c:119: error: request for member ‘nombre’ in something not a structure or union
proyecto2mod.c:120: error: request for member ‘direccion’ in something not a structure or union
proyecto2mod.c:120: error: request for member ‘direccion’ in something not a structure or union
proyecto2mod.c:121: error: request for member ‘num_cliente’ in something not a structure or union
proyecto2mod.c:122: error: request for member ‘ant’ in something not a structure or union
…
И так далее.
Вот коды:
ОСНОВНОЙ КОД
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "clientes.h"
#include "ventas.h"
extern void agrega_nombre(ApLista1 inicio, ApLista1 aux,ApLista1 nodo, ApLista1 aux2);
extern void busca_cliente(ApLista1 inicio, ApLista1 aux,ApLista1 nodo, ApLista1 aux2, ApLista2 inicioventas, ApLista2 auxventas, ApLista2 nodoventas, ApLista2 aux2ventas);
extern int ordena(ApLista1 inicio, ApLista1 aux,ApLista1 nodo,ApLista1 *aux2);
int main(void)
{
int menu=0;
//LISTA1 *aux2,*nodo;
ApLista1 inicio, aux, nodo, aux2;
ApLista2 inicioventas, auxventas,nodoventas,aux2ventas;
inicio=NULL;
nodo=malloc(sizeof(LISTA1));
aux=malloc(sizeof(LISTA1));
inicioventas=NULL;
nodoventas=malloc(sizeof(LISTA2));
auxventas=malloc(sizeof(LISTA2));
system("clear");
while(menu!='6')
{
//funcion void carga_datos();
printf("TIENDA DEPARTAMENTALnnn");
printf("1. Agregar a un Cliente.n2. Agregar una venta.n3. Buscar Cliente. n4. Borrar Cliente. n5. Borrar una venta. n6. Salir.");
printf("nnElige una opcion: ");
scanf("%d", amp;menu);
switch(menu)
{
case 1:
system("clear");
printf("opcion1n");
agrega_nombre(inicio, aux,nodo, aux2);
menu=0;
break;
case 2:
system("clear");
printf("opcion2n");
///lo mismo que agrega_cliente pero utilizando la lista de ventas.
menu=0;
break;
case 3:
system("clear");
printf("opcion3n");
busca_cliente(inicio,aux,nodo,aux2,inicioventas, auxventas, nodoventas, aux2ventas);
menu=0;
break;
case 4:
system("clear");
printf("opcion4n");
//borra_cliente(inicio,aux,nodo,aux2,inicioventas, auxventas, nodoventas, aux2ventas);
menu=0;
break;//
case 5:
system("clear");
printf("opcion5n");
//Lo Mismo que borra cliente pero en la lista de ventas solamente.
menu=0;
break;
case 6:
system("clear");
printf("Adios!n");
exit(1);
menu=0;
break;
}
}
}
Функции, которые идут в main:
void agrega_nombre(ApLista1 *inicio, ApLista1 aux,ApLista1 nodo, ApLista1 aux2)
{
FILE *clientes;
int pops=0, sanders=0;
char aux_char[80];
system("clear");
clientes=fopen("clientes.txt", "a ");
//fseek(clientes, 0L, SEEK_END);
if(nodo==NULL)
{
printf("No hay memorian");
exit(1);
}
printf("NOMBRE:");
scanf("%s", nodo->nombre);
printf("DIRECCION:");
scanf("%s",nodo->direccion);
printf("NUMERO DE CLIENTE:");
scanf("%d", amp;nodo->num_cliente);
if(inicio==NULL)
{
(*inicio)=nodo;
(*inicio)->sig=NULL;
(*inicio)->ant=NULL;
aux=(*inicio);
}
else
{
aux->sig=nodo;
nodo->ant=aux;
aux=nodo;
nodo->sig=NULL;
}
pops ;
aux2=(*inicio);
while(aux2!=NULL)
{
fprintf(clientes, "%sn", aux2->nombre);
fprintf(clientes, "%sn", aux2->direccion);
fprintf(clientes, "%dn", aux2->num_cliente);
puts(aux2->nombre);
puts(aux2->direccion);
aux2=aux2->sig;
}
aux2=aux;
do
{
puts(aux2->nombre);
aux2=aux2->ant;
}
while(aux2!=NULL);
fclose(clientes);
}
//Funcion Busqueda
void busca_cliente(ApLista1 inicio, ApLista1 aux,ApLista1 nodo, ApLista1 aux2, ApLista2 inicioventas, ApLista2 auxventas, ApLista2 nodoventas, ApLista2 aux2ventas)
{
char nombrebusqueda[80];
int numclienteprueba;
inicio=NULL;
printf("Dame el nombre del cliente a buscarn");
scanf("%s",nombrebusqueda);
aux2=aux;
while(aux2!=NULL)
{
printf("Entramos al While Aux2");
if((strcmp(nombrebusqueda,aux2->nombre))==0)
{
printf("Encontramos BAM");
puts(aux2->nombre);
////////Buscamos la venta
aux2ventas=auxventas;
while(aux2ventas!=NULL)
{
numclienteprueba=aux2ventas->num_clienteventas;
if(numclienteprueba==aux2ventas->num_clienteventas)
{
printf("Wow");
///Aqui ensenamos las ventas que ha tenido el cliente y la i informacion de la estructura.
}
aux2ventas=aux2ventas->sigvent;
}
////////////
}
aux2=aux2->sig;
}
}
//Funcion Ordena
int ordena(ApLista1 inicio, ApLista1 aux,ApLista1 nodo,ApLista1 *aux2)
{
char pruebaman[80],probaman[80];//nombres
char pruebadireccion[150],probadireccion[150];//direccion
int pruebanum_cliente,probanum_cliente;//numero de cliente
aux2=aux;
strcpy(pruebaman,aux2->nombre);
strcpy(pruebadireccion,aux2->direccion);
pruebanum_cliente=aux2->num_cliente;
aux2=aux2->ant;
do
{
strcpy(probaman,aux2->nombre);
if(pruebaman[0]<probaman[0])
{
strcpy(pruebaman,aux2->nombre);
strcpy(pruebadireccion,aux2->direccion);
pruebanum_cliente=aux2->num_cliente;
aux2=aux2->sig;
strcpy(aux2->nombre,probaman);
strcpy(aux2->direccion,probadireccion);
probanum_cliente=aux2->num_cliente;
}
strcpy(pruebaman,aux2->nombre);
strcpy(pruebadireccion,aux2->direccion);
pruebanum_cliente=aux2->num_cliente;
aux2=aux2->ant;
printf("Ciclonn");
}
while(aux2!=NULL);
aux2=aux;
strcpy(pruebaman,aux2->nombre);
aux2=aux2->ant;
strcpy(probaman,aux2->nombre);
if(pruebaman[0]<probaman[0])
ordena(inicio,aux,nodo,aux2);
}
Структуры:
#include<stdio.h>
typedef struct clientes{
char nombre[80];
char direccion[150];
int num_cliente;
struct cliente*sig;
struct clientes *ant;
struct ventas *vent;
}LISTA1;
typedef struct clientes *ApLista1;
Вторая структура:
#include<stdio.h>
typedef struct ventas{
int num_clienteventas;
char nombre_producto[150];
int precio;
int unidades;
char fecha[11];
struct ventas *sigvent;
struct ventas *antvent;
}LISTA2;
typedef struct ventas *ApLista2;
Ответ №1:
В int ordena()
вы определяете aux2
как an ApLista1*
, который в основном является a struct clientes**
. Затем вы пытаетесь назначить его в aux
, который определяется как ApLista1 aux
, а именно a struct clientes*
— обратите внимание на пропущенную звезду.
aux2
будучи указателем на указатель на структуру, понятно, почему вызовы like aux2->nombre
не компилируются. Решение состоит в том, чтобы либо определить aux2
as ApLista1
, либо разыменовать его, когда это необходимо.
О, и в качестве примечания — IMO, скрывать *
s за typedef
s не очень хорошая практика. Это не помогает вам печатать меньше, а делает код менее читаемым — и, как в данном случае, более подверженным ошибкам.
Комментарии:
1. 1 при включении
*
в typedef. Это часто приводит к путанице в коде, если только тип четко не указывает, что это указатель, подобныйlp
префиксу Microsoft, или использующийptr
илиhandle
где-то в имени.