Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
int doesn't seem to work with struct and I don't know why. I did the same thing as before and it worked but now it doesn't.
This is the main code.
int main()
{
struct elemente {
char *prod[20];
int cod[20];
int cant[20];
int pret[20];
};
struct elemente a[20];
int i,n=1,p[20];
char *val[20];
for(i=1;i<=n;i++){
puts("Numele produsului");
scanf("%s",&a[i].prod);
puts("Codul");
scanf("%i",&a[i].cod);
puts("Cantitatea");
scanf("%i",&a[i].cant);
puts("Pretul");
scanf("%i",&a[i].pret);
}
It works and I dont see it having problems.
This is where the problem is.
puts("Scrieti numele produsului");scanf("%s", &val);
for(i=1;i<=n;i++){
if(strcmp(val,a[i].prod)==0){
printf("Codul produsului: %i\n", a[i].cod);
printf("Cantitatea: %i\n", a[i].cant);
printf("Pretul: %i\n", a[i].pret);
//p[i]=a[i].cant*a[i].pret;
//printf("Valoarea totala a elementelor %i\n",p[i]);
}
}
The strcmp works fine. But it cannot find the integer numbers I have input with my scanf. It shows a strange code like "2303134". What did I do wrong?
Also as you can see I need to multiply 2 functions but CodeBlocks has problems with the * symbol. How can I fix this?
The problem is that your struct contains arrays of each element instead of a single one. This is also why the multiplication a[i].cant*a[i].pret won't compile, because you're attempting to multiply two int [20] instead of 2 int.
Since you create an array of struct elemente, you only need to input one element in each one:
struct elemente {
char prod[50];
int cod;
int cant;
int pret;
};
You would then change the scanf call to read in prod as follows to make sure you don't read more characters than the value can handle:
scanf("%49s",a[i].prod);
Also, be sure to check the return value of scanf to see whether a value was actually read in.
The strange code, in
printf("Codul produsului: %i\n", a[i].cod) case, is because you try to print the address of a[i].cod instead of the value of integer.
The easiest way to fix this is modify your struct to:
struct elemente {
char *prod[20];
int cod;
int cant;
int pret;
};
It seems there's no requirement to assign int arrays in your struct, int variables are sufficient.
After this modification,
p[i]=a[i].cant*a[i].pret;
printf("Valoarea totala a elementelor %i\n",p[i]);
should be ok to work.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
First of all, this code is running without any issue on my linux desktop pc (x86_64) but on my Cyclone v (arm cpu/fpga), I have a segmentation fault because the value of the pointer is changing. The relevant line is the last one, during the for loop, the value of "layer->filename" is changing, it is correct during the first iteration (the address given by malloc) but it changes on the second one.
Basically, this bit of code is copying character from "buff" to "layer->filename", as you can see in the output file, the value of buff[i] is a valid character so it should not corrupt layer->filename.
If you have an idea of what could cause this issue, please let me know.
Thank you for your help.
typedef enum
{
CONV,
BN,
FC,
} layer_type;
typedef struct layer{
layer_type layer_type;
int shape[3];
char *filename;
} layer;
...
layer *layer=malloc(sizeof(layer));
char buff[30];
int i;
...
layer->filename = malloc(sizeof(char)*(n+1));
if(buff[0]=='b')
layer->layer_type=BN;
else if(buff[0]=='c')
layer->layer_type=CONV;
else
layer->layer_type=FC;
for(i=0; i<n ; i++)
*(layer->filename+i)=buff[i];
values of buff[i] and layer->name during the loop
Using this code
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
CONV,
BN,
FC,
} layer_type;
typedef struct layer{
layer_type layer_type;
int shape[3];
char *filename;
} layer;
size_t test(size_t x) {
printf("%d\n", (int)x);
return x;
}
int main(void) {
layer *layer=malloc(test(sizeof(layer)));
return 0;
}
You can see that sizeof(layer) in the line
layer *layer=malloc(sizeof(layer));
is not the size of a structure but the size of a pointer.
This is because the name of variable is the same as the type name and the compiler treated layer in sizeof as the variable (pointer) name.
To avoid this and have it allocate the size of structure, you should change the name of type or variable to avoid confusion.
Dereferincing the pointer
layer *layer=malloc(sizeof(*layer));
also can solve this problem, but I think renaming is better.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to write a simple c code to create a garden structure,input details like num_animals, type of garden and size of garden.
However, my code is giving me some garbage output.
#include <stdio.h>
#include <string.h>
struct Garden
{
int num_animals;
int size;
char type[10];
};
void Input(struct Garden gardname)
{
printf("\nEnter number of animals\n");
scanf("%d",&gardname.num_animals);
printf("\nEnter size\n");
scanf("%d",&gardname.size);
printf("\nEnter type\n");
scanf("%s",gardname.type);
}
void Output(struct Garden gardname)
{
printf("Num of animals:%d\n",gardname.num_animals);
printf("size:%d\n",gardname.size);
printf("type:%s\n",gardname.type);
}
int main()
{
struct Garden Lumbini;
Input(Lumbini);
Output(Lumbini);
return 0;
}
The functions you have defined, by default use call by value method of passing arguments, which means that although you have Lumbini structure to which you intend to read and write elements, your functions will instead make a copy of Lumbini(in this case gardname), and write to, or print out that copy, not Lumbini. It may lead to duplicate values. I have written the same program using call by reference (note how a pointer to structure is the expected parameter in function, and the reference of lumbini is passed on call). This insures that there are no duplicates, and only the relevant structure (in this case, lumbini) is being manipulated.
#include <stdio.h>
#include <string.h>
struct Garden
{
int num_animals;
int size;
char an_type[10];
};
void Input(struct Garden *gardname)
{
printf("\nEnter number of animals\n");
scanf("%d", &gardname->num_animals);
printf("\nEnter size\n");
scanf("%d", &gardname->size);
printf("\nEnter type\n");
scanf("%s", gardname->an_type);
}
void Output(struct Garden *gardname)
{
printf("Num of animals:%d\n",gardname->num_animals);
printf("size:%d\n",gardname->size);
printf("type:%s\n",gardname->an_type);
}
int main()
{
struct Garden Lumbini;
Input(&Lumbini);
Output(&Lumbini);
return 0;
}
You are getting garbage values because gardname in Output function is a local variable to an uninitialized copy of lumbini structure. When you print out the values of gardname, they will be garbage values as they are not initialized. As the function finishes execution, gardname is destroyed.
Also, instead of writing
scanf("%d",&gardname.num_animals)
you must write
scanf("%d", &gardname->num_animals)
This will read into the address of pointer gardname plus the offset of member num_animals into the structure.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
[EDIT] The main problem was that the fitness of my evolution returned the same value every time after changing some int into float values. The misterious point is that i restarted the computer and it surprisingly worked again.
I'm calling a function from my main, when i debug the code, the variables contain data, but in the header of the function, when i debug, my data is lost and the reference on memory is the same (i'm compiling with visual Studio 2013), this only happens in some of the variables (you can check which ones in the pictures below)
int main(){
float resultados[NUMCROMOSOMAS][CANTIDADMEDICIONES];
int in[CANTIDADMEDICIONES][NUMVAR];
char gramaticas[NUMCROMOSOMAS][LONGITUDCADENA];
int mejorValorIndividuo[100];
char variableNames[NUMVAR + 1];
float fitness[NUMCROMOSOMAS];
char mejorindividuo[LONGITUDCADENA];
float medicionesObtenidas[NUMCROMOSOMAS][CANTIDADMEDICIONES];
int i,j;
(Initializations, some of the relevant ones are)
for (i = 0; i < NUMCROMOSOMAS; i++)
fitness[i] = 0.0;
for (i = 0; i < CANTIDADMEDICIONES; i++)
in[i][0] = i;
Yeah, that was a bidimensional array using one column
And here is the main loop of my program
int curr = MAXINT;
i = 0;
while ( isNotGoodEnough(curr) ){
i++;
curr = generacion(poblacion, results, input, collectedData, gramaticas, mejorindividuo, variableNames, fitness);
}
return poblacion[0][0];
}
The header of my function is this:
int generacion(int poblacion[NUMCROMOSOMAS][SIZECROMOSOMAS],
float resultados[NUMCROMOSOMAS][CANTIDADMEDICIONES],
int in[CANTIDADMEDICIONES][NUMVAR],
float valoresEsperados[NUMCROMOSOMAS][CANTIDADMEDICIONES],
char gramaticas[NUMCROMOSOMAS][LONGITUDCADENA],
char * mejorIndividuo,
char variableNames[NUMVAR],
float fitness[NUMCROMOSOMAS]){
Here is the compiler before the calling
Here is the compiler right after the calling
What i'm doing wrong?
When you have an argument declaration like
int in[CANTIDADMEDICIONES][NUMVAR]
that's not really what the compiler uses, what it translates it to is
int (*in)[NUMVAR]
In other words in is a pointer and not an array.
What you're seeing in the debugger in the function is the pointer, but since the size of the data pointed to by the pointer is unknown the debugger can't show you the data directly. If you explicitly, in the debugger when in the function, check in[0] you will see that the data is correct.
In other words, it's not a problem with the code, it's how the debugger displays (or rather doesn't display) the data.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please help, I need to read an input txt file into an array and print it out put somehow I keep getting error message.
#include <stdio.h>
void reading_into_array(int A[]);
#define MAXVALS 100
int
main(int argc, char *argv[]){
int numbers[100], i;
reading_into_array(numbers[MAXVALS]);
for(i = 0; i < 100; i++){
printf("%d", numbers[i]);
}
return 0;
}
/*input information*/
void
reading_into_array(int A[]){
double inp;
int n = 0;
while(scanf("%lf",&inp) == 1){
A[n++] = inp;
}
}
numbers[MAXVALS] is out-of-range and its type doesn't match with the function argument. use numbers instead.
Avoid using values of uninitialized variables having automatic storage duration, which invokes undefined behavior. Initialize numbers like int numbers[100]={0},i;
When calling a function that takes an array as a parameter, you only need to supply the name of the array, e.g. numbers. "numbers[MAXVALS]" would supply the value of the MAXVALth element of this array. This is wrong for two reasons:
the function needs an array, not an element
The array has a size MAXVAL; its elements are counted from zero to MAXVAL-1, so the MAXVALth element does not even exist
If you want floating point numbers in your array, declare the array as double A[MAXVAL] everywhere.
Last note: the reading_into_array function should have a check that it will prevent it from putting more than MAXVAL numbers into the array, or you risk that it will corrupt memory and crash your program.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to convert assembly to machine code. I have a MINGW compiler where if i type ./convert.exe mov %a then it should output 0x01 0xc0. I am thinking of using a struct listing each assembly code with its corresponding machine value. At the moment i keep getting errors like "request for member opcode in something not a structure". Any help would be appreciated.
#include <stdio.h>
#include <string.h>
struct _Instruction {
char mnemonic[10];
unsigned char opcode;} typedef Instruction;
Instruction instruction_list[] = {
{"mov", 0x01},
{"add", 0x04},
{"sub", 0x05},
{"mul",0x06},
{"div", 0x07},
{"and",0x08},
{"or",0x09},
{"xor",0x0a},
{"cmp",0x0b},
{"",-1},
};
Instruction get_inst(char mnemonic[]);
int main2(int argc, char *argv[])
{
char* instruction = argv[1];
Instruction get_inst = get_Instruction(instruction);
printf("%s ; %s",instruction_list.mnemonic,instruction_list.opcode);
return 0;
}
Instruction get_inst(char mnemonic[])
{
int i;
for(i=0; instruction_list[i].opcode != -1; i++)
{
if(!strcmp(instruction_list[i].mnemonic, mnemonic))
{
return instruction_list[i];
}
}
return instruction_list[i];
}
For one thing, your structure is declared incorrectly. You should format it like this instead:
typedef struct _Instruction {
....
} Instruction;
I'm not sure why that's not triggering syntax errors, but it's certainly not helping.
Also, you have both a variable and a function named get_inst. You call a non-existent function named get_Instruction(). You probably meant to name your function get_Instruction().
Also, the .opcode member of your structure is a single char. Your printf statement uses the "%s" format specifier to print it. This expects a string, which will cause printf to continue reading past the end of the .opcode member, displaying unpredictable garbage and accessing memory it shouldn't be touching.