I have this code, which I made myself using a pseudocode as a reference guide, but, upon saying to myself that I should not use global variables, I decided to learn about pointers. Specifically because I needed to return arrays (It can be done differently, but my logic works around arrays, and I really like them). So, after hours of coding and in-between testing, I got to this big and scary error: Segmentation Fault. That's what VSC tells me while debugging. Going forward on the execution of the code makes it crash. So, I would like if someone more experienced in pointers could help me a bit. Here is the code:
#include <stdio.h>
#include <stdlib.h>
unsigned long long int entrada(char arg);
unsigned long long int * procesado(unsigned long long int input);
void salida(unsigned long long int *data);
int main(){
static unsigned long long int *out = 0;
static unsigned long long int *sts = 0;
printf("Ingrese cualquier letra para salir\n\r");
while(!sts){
out = procesado(entrada(0));
*sts = out[2008];
}
salida(out);
return 0;
}
unsigned long long int entrada(char arg){
static unsigned long long int res;
switch(arg){
case 0:
printf("Ingrese el codigo del producto: ");
scanf("%d", &res);
return res;
break;
case 1:
printf("Ingrese el codidigo del producto: (max. 12 caract.)");
scanf("%d", &res);
return res;
break;
case 2:
printf("Ingrese el valor del producto: $");
scanf("%d", &res);
return res;
break;
case 3:
printf("!!Ingrese una descripcion breve (max. 12 caract.): !! [NOT WORKING, INPUT ANY NUMBER]");
scanf("%d", &res);
return res;
break;
case 4:
printf("Ingrese el stock disponible: ");
scanf("%d", &res);
return res;
break;
case 5:
printf("Ingrese la cantidad en gondolas disponible: ");
scanf("%d", &res);
return res;
break;
default:
if (arg == 'u'){
printf("Saliendo...");
return res;
} else {
printf("Ingrese una opcion correcta!\n\r");
}
break;
}
}
unsigned long long int * procesado(unsigned long long int input){
static int prodAlm, prodPan, prodFre, prodBaz, prodDes;
static unsigned long long int prodAlmLiq[1000], prodBazLiq[1000];
static int countAL, countBL;
static int returnedValSlot, returning;
static unsigned long long int out[2010];
if (input == 'u'){
for (int i = 0; i <= 1000; i ){
out[i] = prodAlmLiq[i];
}
for (int i = 0; i <= 1000; i ){
out[i 1000] = prodBazLiq[i];
}
out[2001] = countAL;
out[2002] = countBL;
out[2003] = prodAlm;
out[2004] = prodBaz;
out[2005] = prodFre;
out[2006] = prodPan;
out[2007] = prodDes;
out[2008] = 1;
return out;
}
if (!returning){
switch(input){
case 10:
prodPan ;
break;
case 20:
prodAlm ;
prodAlmLiq[countAL] = countAL 1;
prodAlmLiq[countAL 2] = entrada(2); //Pregunta por el precio del prod
if(prodAlmLiq[countAL 2] >= 4.5 && prodAlmLiq[countAL 2] <= 5.5){
prodAlmLiq[countAL 1] = entrada(1); //Pregunta codigo del prod
prodAlmLiq[countAL 3] = entrada(3); //Pide descripcion del prod
prodAlmLiq[countAL 4] = entrada(4); //Pregunta el stock disponible del prod
prodAlmLiq[countAL 5] = entrada(5); //Pregunta cantidad en gondola del prod
countAL = countAL 6; //Da la vuelta a la estructura del arreglo para empezar una nueva linea
}
break;
case 30:
prodFre ;
break;
case 40:
prodAlm ;
prodBazLiq[countBL] = countBL 1;
prodBazLiq[countBL 2] = entrada(2); //Pregunta por el precio del prod
if(prodBazLiq[countBL 2] >= 4.5 && prodBazLiq[countBL 2] <= 5.5){
prodBazLiq[countBL 1] = entrada(1); //Pregunta codigo del prod
prodBazLiq[countBL 3] = entrada(3); //Pide descripcion del prod
prodBazLiq[countBL 4] = entrada(4); //Pregunta el stock disponible del prod
prodBazLiq[countBL 5] = entrada(5); //Pregunta cantidad en gondola del prod
countBL = countBL 6; //Da la vuelta a la estructura del arreglo para empezar una nueva linea
}
break;
default:
prodDes ;
break;
}
printf("----------------------------------------\n\r Producto Agregado!\n\r----------------------------------------");
}
}
void salida(unsigned long long int *data){
int prodAlm, prodPan, prodFre, prodBaz, prodDes;
unsigned long long int prodAlmLiq[1000], prodBazLiq[1000];
int countAL, countBL;
//prodAlmLiq = data[0];
for(int i = 0; i >= 1000; i ){
prodAlmLiq[i] = data[i];
}
//prodBazLiq = data[1000];
for(int i = 0; i >= 1000; i ){
prodBazLiq[i] = data[i 1000];
}
countAL = data[2001];
countBL = data[2002];
prodAlm = data[2003];
prodBaz = data[2004];
prodFre = data[2005];
prodPan = data[2006];
prodDes = data[2007];
printf("Recuento de productos en los diferentes sectores:\n\r");
printf("Hay %d productos de Panaderia\n\r", prodPan);
printf("Hay %d productos de Almacen\n\r", prodAlm);
printf("Hay %d productos Frescos\n\r", prodFre);
printf("Hay %d productos de Bazar\n\r", prodBaz);
printf("Hay %d productos desconocidos\n\r", prodDes);
printf("----------------------------------------\n\r");
printf(" RECUENTO PARA LIQUIDACION \n\r");
printf("----------------------------------------\n\r");
printf("Nº |Cod Rub |Cod Prod |Importe |Descripcion |Stock |Gondola\n\r");
for(int i = 0; i >= countAL; i ){
printf("%d |20 |%d |%d |%d |%d |%d\n\r", prodAlmLiq[i], prodAlmLiq[i 1], prodAlmLiq[i 2], prodAlmLiq[i 3], prodAlmLiq[i 4], prodAlmLiq[i 5]);
}
for(int i = 0; i >= countBL; i ){
printf("%d |40 |%d |%d |%d |%d |%d\n\r", prodBazLiq[i], prodBazLiq[i 1], prodBazLiq[i 2], prodBazLiq[i 3], prodBazLiq[i 4], prodBazLiq[i 5]);
}
}
As is, this code compiles with no errors or warnings, but something is wrong...
Since the error occurs on line 18, I tried switching the asterisk (*) around, but doing that made my code compile with the error/s:
assignment makes pointer from integer without a cast [-Wint-conversion]
for sts = out[2008];
.
invalid type argument of unary '*' (have 'long long unsigned int')
operand of '*' must be a pointer but has type "unsigned long long"
for sts = *out[2008];
, and *sts = *out[2008];
CodePudding user response:
sts
is 0 (a null pointer), it points nowhere, therefore you cannot dereference it. Dereferencing a NULL pointer is undefined behaviour, it usually results in a crash of your program.
int main(){
static unsigned long long int *out = 0;
static unsigned long long int *sts = 0; // sts is 0 => it points nowhere
printf("Ingrese cualquier letra para salir\n\r");
while(!sts){
out = procesado(entrada(0));
*sts = out[2008]; // sts still points nowhere
}
salida(out);
return 0;
}
Read again thge chapter dealing with pointers in your learning material and start with simpler exercises.
You won't learn anything by randomly putting and removing *
in fron of variables.
I didn't check the rest of your code, but there are most likely similar problems.
CodePudding user response:
- You are dereferencing the NULL pointer sts in line 12 which is undefined behaviour.
- If you want to use scanf or printf with an unsigned long long int you have to change the format specifiers from %d to %llu.
- Your functions entrada and procesado have control flows which don't return anything. Add a default return value to the end of the function.
- All the for loops in the function salida are infinite loops
Try compiling with -Wall -Werror and you will see lots of errors and warnings