Posts by pmg • 6,456 points
254 posts
-
5
votes1
answer179
viewsA: Segmentation fault: branch and bound in c
k = vetor[i].altura; j = vetor[i].largura; lab[k][j] = 5; /****** O erro ocorre aqui *******/ If I interpreted your code correctly (I did not read it carefully) the error occurs because you try to…
-
2
votes2
answers775
viewsA: How to Vector/Array integers indexed by strings?
In C is not possible (I do not know C++). In C the indices of arrays are obligatory integers. What you can do is convert the string to a (single) number and use that number as input for the array…
-
-1
votes1
answer896
viewsA: Opening and reading giant C files
char **xxxx; if ((arq = fopen("Meuarquivo.txt", "r+b")) == NULL) { printf("Houve um erro na abertura do arquivo"); getche(); exit(1); } int c = 0; xxxx =…
-
3
votes2
answers3525
viewsA: Floating point Exception (core dumped)
dinheiro.valorquina = 150600 / z; dinheiro.valorquadra = 30000 / n; dinheiro.valorterno = 20000 / o; If z or n or o are zero at the time of this operation, the program stops with the error "Floating…
-
1
votes2
answers394
viewsA: Function giving wrong values
for(i = 0, contadorI = 0; i < 30; i++, contadorI += 2) // ^^^^^^^^^^^^^^ { for(c = 0, contadorC = 0; c < 30; c++, contadorC += 2) // ^^^^^^^^^^^^^^ { dij = distancia(coordenadasX[contadorI],…
-
1
votes3
answers2790
viewsA: Read a string with line break
You can remove the line break in the first string (in a copy if necessary) fgets(exemplo, sizeof exemplo, stdin); // assume no errors len = strlen(exemplo); if ((len > 0) && (exemplo[len…
-
1
votes1
answer93
viewsA: Error when I try to call a function
int cX, cY; aux = distancia(coordenadasX[j], cX[j], coordenadasY[j], cY[j]); // ????? ????? Nor cX nor cY are arrays. You can’t use it that way.…
-
0
votes3
answers272
viewsA: Incompatible types?
To assign values to character arrays (to strings) you must use strcpy(). #include <string.h> ... // d[i].cidade = cidadeXY[i]; strcpy(d[i].cidade, cidadeXY[i]);…
-
1
votes2
answers1770
viewsA: Sort a struct
Uses the function qsort() of the standard library with its own sorting function int orderbyx(const void *a, const void *b) { const struct cidadecoord *aa = a; const struct cidadecoord *bb = b;…
-
3
votes1
answer142
viewsA: Saving stream in a file
info = realloc(info, (tam + 1) * sizeof (struct end)); // ^ ^ But you’ll waste memory for one object. How do you increase the tam before the realloc don’t need to add 1. Don’t forget that in an…
-
2
votes1
answer56
viewsA: Stream corrupting to be saved
info = realloc(info, 1); This line makes it info Now point to a memory space with 1 byte of size, which is not enough for several objects of the type struct end. You have to know how many elements…
-
1
votes3
answers133
viewsA: Marathon Problem: Calculator (Level 2)
Check the scanfs in main. If the input does not start correctly, your program does not give error message. if (scanf("%lf", &num1) != 1) { fprintf(stderr, "operacao nao pode ser realizada\n");…
-
1
votes3
answers133
viewsA: Marathon Problem: Calculator (Level 2)
Try adding a Newline in the two error printf. printf("operacao nao pode ser realizada\n"); // ^^ In some systems, the absence of Newline makes the line not printed or recognized by the parent…
-
2
votes1
answer1328
viewsA: String with memory junk
First thing I noticed int top_IDS(int ID_DNA, char *linhas[]) { FILE *arquivo; arquivo=fopen("MEL_PIOR.txt","a"); fflush(stdin); fputs(linhas,arquivo); ... 0) Do not check whether the fopen() worked…
-
2
votes1
answer652
viewsA: Fraction sequence with Fibonacci and primes
Instead of calculating and printing; calculates and puts in arrays. Then prints the arrays. int arrf[10]; // array para fibonacci int arrp[10]; // array para primos // calcula e guarda numeros for…
-
1
votes2
answers52
viewsA: You’re not stopping file processing
After reading the string with fgets() remove from the '\n'. ret = fgets(nomeS,sizeof(nomeS), stdin); if (ret == NULL) /* erro */; nomelen = strlen(nomeS); if (nomelen == 0) /* erro estranho */; if…
-
0
votes2
answers115
viewsA: Segmentation Fault in Structs C array
The size of each element is wrong. //grafo = (Node*) malloc((numVertices) * sizeof(struct Node*)); grafo = malloc(numVertices * sizeof *grafo);
-
0
votes1
answer1440
viewsA: Recursive function to end a list
After making free() the contents of the variable have an invalid value. I advise you to replace this invalid value with NULL. void finaliza_recursivo(no **inicio) { if(*inicio != NULL)…
-
4
votes1
answer719
viewsA: C print name with scanf
Experiment with arrays instead of pointers #include <stdio.h> int main(void) { char string[1000]; // array em vez de ponteiro char string2[1000]; // array em vez de ponteiro printf("Primeiro…
-
0
votes2
answers352
viewsA: Segmentation failure in malloc() function
I suggest you simplify the instruction *ptr[x] = malloc(sizeof(***ptr) * sizeofline(file)); For example to size_t tmp1 = sizeofline(file); *ptr[x] = malloc(tmp1); // sizeof(***ptr) == 1 and now…
-
4
votes5
answers7954
viewsA: Removing the " n" from a string read using fgets()
The best way, in my opinion, is to read the full string and then remove the '\n' char input[1000]; size_t len; if (!fgets(input, sizeof input, stdin)) { /* tratamento de erro */ } len =…
-
3
votes2
answers276
viewsA: Error when trying to pass a struct by C reference?
The first time the compiler finds the function imprime() he has not yet found the definition of struct aluno. Then, inside the function main() the definition of the struct aluno; which shall take…
-
2
votes2
answers121
viewsA: Can I return a struct by initiating it into a Return in ANSI C?
Copia (with my translation) da Ouah’s answer on Stack Overflow in English. You can do it using one literal compound (composite literal): Result test(void) { return (Result) {.low = 0, .high = 100,…
-
2
votes1
answer1004
viewsA: How to check winner in the game of old
Your job vencedor() has no instruction return.
-
3
votes2
answers84
viewsA: How does the compiler work in the case of a casting like this?
Of your four printf only 1 is correct. The other three do Undefined Behaviour printf("%d\n", (unsigned int) (x - i)); // UB printf("%u\n", (int) (x - i)); // UB printf("%d\n", x - i); //…
-
1
votes2
answers93
viewsA: What is the difference between these expressions?
When you add the header that contains the function declaration srand() (the header stdlib.h) compiler knows what type of parameter. Then it converts the type you used to the required type, if…
-
2
votes2
answers329
viewsA: Store SMS message in variable
I think that 'ÿ' be of value -1. According to the documentation of SoftwareSerial: read Returns the Character read, or -1 if None is available if the function returns -1 means that no characters are…
-
4
votes1
answer457
viewsA: Matrix multiplication in C
When j for 3, the if will access a non-existent element if(mat[i][j]>=mat[i][j+1]) // mat[i][4] nao existe
-
3
votes1
answer123
viewsA: Can be improved
Performance of prime numbers: you do not need to see if it is multiples of 1 or of itself; in the end you check whether the total of divisors is 0 (instead of 2). do not need to check dividers below…
-
1
votes2
answers576
viewsA: Fscanf, fprintf and printf
I haven’t been going over your code, but I have two pieces of advice 1) Checks the result of scanf() with the number of assignments //while((fscanf(mestre,"%s%s%.2f%.1d%s", ch1, ch2, fl1, it1,…
-
1
votes4
answers5957
viewsA: Error in code, C
collect2.exe: error: ld returned 1 exit status ^^ ld is the "Linker": the part of the compiler that 'mixes' your code with the existing code (the scanf(), for example). Apart from the &A used in…
-
3
votes1
answer130
viewsA: Error in Visual Studio
Using the i like Indice an array, this seems wrong for(i = 1; i <=n; i++) // h[0] nao acedido // tentativa de aceder a h[n], que nao existe??? The arrays, in C, go from the Dice 0 to the Indian…
-
1
votes2
answers371
viewsA: Eliminating extra characters using scanf
Use the return of scanf() if (scanf("%14[^;];%40[^;];%8[^;];%2[^;\r\n]%*[;\r\n]", cnpj, razao_social, data_de_fundacao, uf) != 4) /* erro */;…
-
2
votes4
answers409
viewsA: How to transform, for example, "0" to "ZERO" in C? What problem in this code?
It is not possible to assign values to arrays. You need to assign array elements one by one. The C language has some shortcuts to facilitate this assignment when it comes to strings. num1[0] = 'Z';…
-
1
votes1
answer71
viewsA: Counting a given day period with more vehicles
According to the title of the post, you must need an array to count by period. Each element of the array corresponds to a period. For example, if the period is 1 hour int contaperiodo[24]; //foreach…
-
2
votes3
answers665
viewsA: Register with C File
What do you mean, "if he exists"???? If the result of fread() has been 1, the object registro has been filled in with information from stream; if the result was 0 there was an error that you can…
-
4
votes1
answer895
viewsA: Problem with "Double" on my calculator I made in "C" language
A few points to help you always use double for variables or floating point functions is customary for prototypes of functions outside the function main() The converter for input of type values…
-
1
votes1
answer58
viewsA: Can the iv used in AES-CTR be stored in clear?
A wikipedia article says (the translation is mine): A boot vector has different security requirements than a key, so IV doesn’t need to be secret. However, in most cases, it is important that the…
-
2
votes2
answers580
viewsA: Struct with pointer and allocation
After allocating memory to video, you have to allocate to video->cliente, for video->filme, video->emprestimo, and video->devolucao. int main() { Emprestimo *video; video = malloc(regE *…
-
1
votes2
answers153
viewsA: reset problem in C code
Mete if (scanf("%f%f%f", &CP, &LA, &AA) != 3) /* erro */; Instead of scanf("%f,%f, %f", &CP, &LA, &AA); The conversion "%f, %f, %f" means to read an optional float preceded…
-
6
votes2
answers1552
viewsA: Is it possible to implement a dynamic array within a C structure?
Yes. Uses pointers and malloc()/realloc() and free() #include <stdlib.h> // malloc(), realloc(), free() struct historico_de_fabrica { Hist_id_rolos *rolos; }; struct historico_de_fabrica obj;…
-
2
votes1
answer620
viewsA: Function count comments and another program per C command line
For a program with reduced needs like yours, you don’t need to complicate. The arguments of function main() (the argc and the argv) indicate respectively how many and what were these arguments. If a…
-
3
votes2
answers967
viewsA: How to read a random amount of integers in C?
Your problem and the use of scanf() while(scanf("%li", &numero) != EOF) The value that the scanf() return usually corresponds to the number of assignments performed (in your case this value is 1…
-
1
votes2
answers5760
viewsA: Read multiple integers in the same input in C
long int *vetor = malloc(numeros*sizeof(long int)); scanf("%i", &vetor[i]); To read long int you can’t use "%i". Or declare your array as int *vetor, or use "%li" in the scanf. If the problem…
-
12
votes4
answers6387
views -
5
votes2
answers103
viewsA: How to create a vector2 in C?
In C, with struct (C has not class), do so /* define struct Vector2 com dois elementos */ struct Vector2 { int x; int y; }; /* define tile como uma variavel pertencente ao tipo struct Vector2 */…
-
1
votes2
answers1172
viewsA: Error from "expected Expression before ːint'"
To make a "cast" use the desired type between parentheses followed by the value (int)tempo // cast de tempo para tipo int: correcto int(tempo) // incorrecto: aparenta ser a chamada da funcao int com…
-
3
votes4
answers616
viewsA: Trouble finding prime numbers
Let’s see what happens with an example // a b i scanf("%d", &a); // 3 i = a; // 3 3 while (i > 1){ // OK b = a / (i - 1); // 3 1 3 i = i - 1; // 3 1 2 if (a % (i - 1) == 0){ // o resto da…
-
5
votes2
answers5124
viewsA: Copy Strings in C
matriz is not a string: it is an array of strings. You can’t use matriz directly in office str*; you have to use its elements. The third element of matriz is matriz[2], that element is (or rather,…
-
5
votes4
answers8703
viewsA: Programming Online in C?
For basic programs you have the codepad.org. Much like that, you have the ideone. that has the functionality to accept input.…