When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
39 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
!f
@ripe saddle's code (missing deletion permissions), requested by @hot hull:
Hello, sorry, could someone help me to know my error in this code to insert in a simple list please
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nodo {
int id;
char tipoTaco[20];
int cantidadTacos;
char saborAgua[20];
int cuenta;
struct nodo* sig;
} lista;
void insertar(lista** ptr,
int id,
char* tipoTaco,
int cantidadTacos,
char* saborAgua,
int cuenta) {
lista *pt1, *pt2;
pt1 = *ptr;
if (pt1 == NULL) {
pt1 = (lista*)malloc(sizeof(lista));
if (pt1 != NULL) {
pt1->id = id;
strcpy(pt1->tipoTaco, tipoTaco);
pt1->cantidadTacos = cantidadTacos;
strcpy(pt1->saborAgua, saborAgua);
pt1->cuenta = cuenta;
pt1->sig = NULL;
*ptr = pt1;
} else {
printf("No se pudo crear el nodo\n");
}
} else {
while (pt1->sig != NULL)
pt1 = pt1->sig;
pt2 = (lista*)malloc(sizeof(lista));
if (pt2 != NULL) {
pt2->id = id;
strcpy(pt1->tipoTaco, tipoTaco);
pt1->cantidadTacos = cantidadTacos;
strcpy(pt1->saborAgua, saborAgua);
pt1->cuenta = cuenta;
pt2->sig = NULL;
pt1->sig = pt2;
} else {
printf("No se pudo crear el nodo\n");
}
}
}
void ver_lista(lista* ptr) {
while (ptr != NULL) {
printf(" %d\t", ptr->id);
printf(" %s\t", ptr->tipoTaco);
printf(" %d\t", ptr->cantidadTacos);
printf(" %s\t", ptr->saborAgua);
printf(" %d\t", ptr->cuenta);
ptr = ptr->sig;
}
}
}```
You have an extra }
Sorry, I can't send all my code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nodo{
int id;
char tipoTaco[20];
int cantidadTacos;
char saborAgua[20];
int cuenta;
struct nodo *sig;
} lista;
void insertar(lista **ptr, int id, char *tipoTaco, int cantidadTacos, char *saborAgua, int cuenta){
lista *pt1, *pt2;
pt1 = *ptr;
if (pt1 == NULL){
pt1 = (lista *)malloc(sizeof(lista));
if (pt1 != NULL){
pt1->id = id;
strcpy(pt1->tipoTaco, tipoTaco);
pt1->cantidadTacos = cantidadTacos;
strcpy(pt1->saborAgua, saborAgua);
pt1->cuenta = cuenta;
pt1->sig = NULL;
*ptr = pt1;
}
else{
printf("No se pudo crear el nodo\n");
}
}
else{
while (pt1->sig != NULL)
pt1 = pt1->sig;
pt2 = (lista *)malloc(sizeof(lista));
if (pt2 != NULL){
pt2->id = id;
strcpy(pt1->tipoTaco, tipoTaco);
pt1->cantidadTacos = cantidadTacos;
strcpy(pt1->saborAgua, saborAgua);
pt1->cuenta = cuenta;
pt2->sig = NULL;
pt1->sig = pt2;
}
else{
printf("No se pudo crear el nodo\n");
}
}
}
void ver_lista(lista *ptr){
while (ptr != NULL){
printf(" %d\t", ptr->id);
printf(" %s\t", ptr->tipoTaco);
printf(" %d\t", ptr->cantidadTacos);
printf(" %s\t", ptr->saborAgua);
printf(" %d\t", ptr->cuenta);
ptr = ptr->sig;
}
}
//TODO: Por hacer
void ver_lista_id(lista *ptr, int id){
while( ptr != NULL ){
if( ptr->id == id ){
printf(" %d\t", ptr->id);
printf(" %s\t", ptr->tipoTaco);
printf(" %d\t", ptr->cantidadTacos);
printf(" %s\t", ptr->saborAgua);
printf(" %d\t", ptr->cuenta);
}
else{
printf( "\nValor no encontrado..." );
}
}
}
These are my functions
What errors are you running into
int main(void){
lista list;
//struct lista ordenes[3];
int id;
char tipoTaco[20];
int cantidadTacos;
char saborAgua[30];
int cuenta;
printf( "Bienvenido a The Taco :D\n" );
printf( "Ingrese el id del taco: " );
scanf( "%d", &id );
printf( "Ingrese el tipo de taco: " );
scanf( "%s", tipoTaco );
printf( "Ingrese la cantidad de datos: " );
scanf( "%d", &cantidadTacos );
printf( "Ingrese el sabor del agua: " );
scanf( "%s", saborAgua );
printf( "Ingrese la cuenta: " );
scanf( "%d", &cuenta );
insertar( &list, id, tipoTaco, cantidadTacos, saborAgua, cuenta );
ver_lista( &list );
return 0;
}```
Kind of hard to help with code written in another language
typedef struct nodo{
int id;
char tipoTaco[20];
int cantidadTacos;
char saborAgua[20];
int cuenta;
struct nodo *sig;
} lista;
void insertar(lista **ptr, int id, char *tipoTaco, int cantidadTacos, char *saborAgua, int cuenta){
lista *pt1, *pt2;
pt1 = *ptr;
if (pt1 == NULL){
pt1 = (lista *)malloc(sizeof(lista));...
Sorry, I speak Spanish. If there's something you don't understand, tell me and I'll translate it.
Pay attention to compiler warnings
Also there's a tool called address sanitizer which will identify memory errors in your program
It does not save the values as I want 😦
Insertar is Insert
Take a look at this ^^, pay attention to the compiler warnings and output from asan
Okay, the errors are because the arguments are incompatible?
I think I'm a bit confused with the double pointer, how should that argument be passed?
@ripe saddle You should probably have lista* list; in main not lista list;
keep in mind lista is a node itself
Ready, but I don't understand why it is still incompatible 😦
lista** and lista* are very different types
Trying to treat a lista* as a lista** will certainly result in errors
Hence the warning from the compiler
Ok, I understand that they are different, but I don't know how I could treat them to make my program work.
@hot hull By the way, sorry if you don't understand anything I write, I'm using the Google translator
Is this the solution?
int main(void){
lista *list;
int id;
char tipoTaco[20];
int cantidadTacos;
char saborAgua[30];
int cuenta;
printf( "Bienvenido a The Taco :D\n" );
printf( "Ingrese el id del taco: " );
scanf( "%d", &id );
printf( "Ingrese el tipo de taco: " );
scanf( "%s", tipoTaco );
printf( "Ingrese la cantidad de datos: " );
scanf( "%d", &cantidadTacos );
printf( "Ingrese el sabor del agua: " );
scanf( "%s", saborAgua );
printf( "Ingrese la cuenta: " );
scanf( "%d", &cuenta );
insertar( &list, id, tipoTaco, cantidadTacos, saborAgua, cuenta );
ver_lista( list );
return 0;
}```
yep
Typically you never put linked list nodes on the stack, which is what lista list; would be
I didn't understand that part
I already tested the code and it doesn't throw any warning, but it doesn't print the ver_lista function 😦
https://godbolt.org/z/1d3eq7sEq address sanitizer is your friend
insertar isn't quite right
Should the NULL value be set to ptr1?
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.
!solved