Posts by pmg • 6,456 points
254 posts
-
1
votes1
answer309
viewsA: I have a bug in the scanf
The converter "%c" (unlike many other scanf()) does not ignore empty spaces. If the input has spaces (or Enters or Tabs) these spaces will be assigned to the specified variable. To ignore blank…
-
1
votes2
answers1876
views -
2
votes3
answers17331
viewsA: Convert Fractional decimal to binary
how I treat the fractional part in C? As you demonstrated in the example: #include <stdio.h> int main(void) { char output[100] = ""; double x = 0.625; while (x) { int ch; double chk = x * 2;…
-
1
votes2
answers174
viewsA: Use pointer on struct
Agr access the first string of char *produtos[200000] using p->produtos[0] but I wanted to access the first character of the first string. p->produtos[0][0] or *(p->produtos[0])…
-
1
votes1
answer141
viewsA: Two-dimensional array size within the C-struct
With C99 you can use VLA (Variable Length Arrays). But beware not to abuse the sizes! FILE *f; f = fopen(argv[1], "r"); fscanf(f, "%i%i", &numVertices, &numArestas); int…
-
1
votes1
answer69
viewsA: Doubts of logic in C
antecessor=numero-1; sucessor+numero+1; /* esta linha nao faz nada */ printf("=============================================\n\n"); Take a good look at the second line of the above section :)…
-
1
votes1
answer106
viewsA: Infinite loop when passing memory addresses
for (ptrPA = &pa[0] + 1; ptrPA <= &ptrPA[9]; ptrPA++) // ^^^^^^^^ should be for (ptrPA = &pa[0] + 1; ptrPA <= &pa[9]; ptrPA++) // ^^^^^…
-
1
votes1
answer102
views -
1
votes1
answer40
viewsA: Problems in a string array of a structure
When you use char *produtos[200000]; you have an array of (200000) pointers. With your code all those pointers point to the function result aplica(). What you need to do is assign a different value…
-
1
votes1
answer94
viewsA: Run a list of executables
if (x == 0) { execvp (s[i],&s[i]); x = fork(); // <== nunca executa } This second fork() never executes. After the execvp() the current program is replaced by another and the fork() ceases to…
-
2
votes1
answer3898
viewsA: C file manipulation (skip lines)
int random = rand() % 10; random will be 0, or 1, or ... 9 if(x = random) { fscanf(arquivo, "%s %d %s\n", nome, &dano, desc); break; } This if executes whenever random is different from 0. It…
-
3
votes3
answers41
viewsA: Pointed from Pointed C
Imagine you have several text pointers char *lang_pt[] = {"rato", "teclado", "monitor"}; char *lang_en[] = {"mouse", "keyboard", "screen"}; and you want to make a single pointer that points to the…
-
3
votes2
answers288
viewsA: Run the TOP command in exec on C - Linux?
In the snippet if (valorMenu == 1) { printf("Rodando o bin/ls\n"); void ExeclLS(int argc, char **argv); // <== ATENÇÃO } the line with the reminder "does nothing". It is just a statement. To run…
-
2
votes2
answers288
viewsA: Run the TOP command in exec on C - Linux?
The message of perror() /bin/top: no file found indicates that the top is not in the directory /bin. At the command line, do whereis top to find out where the program in question is. $ whereis top…
-
0
votes3
answers72
viewsA: Largest number in array, does not display
int main() { int mat[lin][col], i, j, maior=mat[0][0], pos_i, pos_j; // ^^^^^^^^^ LIXO When you do the assignment to maior the content of mat[0][0] is junk. You have to do that assignment afterward…
-
1
votes4
answers1407
viewsA: How to work with values that far exceed the long (64 bit) limit value
Multiplication can be done by parts. You divide the number into smaller pieces, you multiply those bits (with rules) and finally you add all the products, with rules. For example, to multiply…
-
1
votes2
answers1797
viewsA: Store values of a string in an array
To answer your question: use fgets() to read strings with spaces. Attention that the fgets() stores the final ENTER of each line. If necessary clear this ENTER before processing the string further.…
-
0
votes1
answer219
viewsA: Problems to calculate the largest prime int in C
In function main() flames the function ePrimo() with the value INT_MAX. In function ePrimo() your cycle for (i = 0; i <= n; i++) { /* ... */ } is an infinite cycle. i is at all times less than or…
-
1
votes2
answers105
viewsA: For rule in C
The whole for can be replaced by a while. Maybe you find the syntax of while more intuitive :-) for (initialization; condition; increment) body; for (initialization; condition; increment) {body};…
-
2
votes1
answer31
viewsA: Error collecting va_list value in C
Check if in the version with _mysql_insert() you got the #include <stdarg.h>.
-
0
votes1
answer30
viewsA: Checksum ip Packet
After the initial calculations completed, the variable sum (32 bits) will have any value. The first expression (sum = (sum >> 16) + (sum & 0xffff);) isolates the two 16-bit halves of this…
-
4
votes1
answer670
viewsA: How to use GOTO in C to implement finite automata?
goto Q3; i++; Your i++ will never be executed, for they are after the goto
-
8
votes2
answers180
viewsA: Very large integer does not work
If you have a C99 implementation, you can try unsigned long long. This guy has to exist, but it may not be bigger than unsigned long --- and therefore may not solve your problem. Check with, for…
-
2
votes4
answers385
viewsA: Obtaining the Rest of a Floating Point
#include <math.h> double x = 1.13; double resto = floor((x - floor(x)) * 100); // ^^^^^^^^^^^^ <== 0.13
-
4
votes2
answers1114
viewsA: Line break does not work on file
You have to open the file in text mode arquivo = fopen(nome_arquivo, "w"); // modo sem "b" fprintf(arquivo, "%s\n", info); If, even so, the '\n' not converted to normal line break for your specific…
-
1
votes1
answer1115
viewsA: Problem with split function in C
char to_list[strlen(str)]; The array to_list has space for strings up strlen(str) - 1 length. If you put more characters there (including the '\0') invokes Behavior Indefinite. strcpy(to_list, str);…
-
2
votes3
answers171
viewsA: How to simplify this comparison?
In C, if "strings" are just characters, I would use strchr() char *b; char variavel[100] = "x"; b = strchr("ad...y", *variavel); // usa b como boolean if (b) ok(); If strings are actually strings of…
-
2
votes1
answer226
viewsA: Segmentation fault function fgets
The "open" of arquivo2 and its "close" are not at the same level. One runs within a cycle, another runs outside the cycle. while (/* ... */) { /* ... */ arquivo2 = fopen(/* ... */); /* ... */ } /*…
-
0
votes1
answer81
views -
0
votes2
answers153
viewsA: Why does it not read the string matrix?
The instruction scanf("%[^\n]s", *(nomeEmpresas + i) ); // ^^^ s a mais want to read a string followed by a s. Like the s is part of the string, the scan never ends. Take away the s scanf("%[^\n]",…
-
3
votes2
answers61
viewsA: Why "Segmentation fault " when two arrays are interspersed?
The instructions k += k + 1; j += j + 1; should be k = k + 1; j = j + 1; or k += 1; j += 1; or k++; j++;…
-
2
votes2
answers83
viewsA: how to capture what is printed on the console via a command into a vector in C language
In posix systems you can do what you want with popen(). Possibly there are other specific ways for specific operating systems (Windows, Android, iOS, MINIX, Nextstep, ...) Example #include…
-
8
votes3
answers9258
viewsA: What good is while(0) and while(1) in C?
The while(1) is normally used for infinite cycles while (1) { /* ciclo infinito, por exemplo */ system("stayactive"); // mantem a aplicacao activa, mesmo que ela crashe } The while(0) is usually…
-
1
votes2
answers1071
viewsA: Guess a number, and in attempts, by the percentage show hints
What you need is to compare the difference between target and chosen number and the range of possible numbers. That is, imagining that the right number was 1 and that the user shot 4: for a range…
-
2
votes1
answer3931
viewsA: Vector file content in C
How do you know how many words the file has? My intuition tells me that you have already read the file once, before calling this function, and therefore the internal sharpener of the file is at the…
-
2
votes1
answer330
viewsA: Concatenate string with integer array and write to file?
You don’t need to build the string dynamically in memory: you can use fprintf() and print directly to the archive fprintf(arquivo, "[B]:"); // [B]: for (int k = 0; k < n; k++) { fprintf(arquivo,…
-
1
votes2
answers1128
viewsA: free(): invalid next size (fast) while trying to free up memory
Try it like this tVector *create(int n) { tVector *newVector = malloc(sizeof *newVector); if (!newVector) { fprintf(stderr, "error in the Malloc process for newVector.\n"); exit(EXIT_FILURE); } //…
-
1
votes2
answers324
viewsA: How to declare a component of a static struct in the function? C
Removes the variable definition conect from file h. // ficheiro h struct conectores { int entrada[n]; int busca[n]; }; // sem definicao de variaveis deste tipo Pass this setting to file c. //…
-
2
votes2
answers1679
viewsA: How do I generate random numbers and without repeating them in a time interval?
In C, the usual way to solve the randomness problem without repetition is to put all possible values in an array, shuffle the array and choose the first ones N elements. int limite_inferior,…
-
5
votes2
answers421
viewsA: How to write a recursive function?
What repeats in the function is the cycle while while(!feof(arq)) { y = atoi(strtok(buf, ";")); if (x == y) { fclose(arq); return 1; } fgets(buf, MAX, arq); } So this, with slight changes, needs to…
-
0
votes4
answers274
viewsA: Problem checking prime numbers
Up to 250 makes a table ... #include <stdio.h> #include <stdlib.h> #include <time.h> int diff(const void *, const void *); static int primo[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,…
-
4
votes2
answers1763
viewsA: Comparing user typed variable with file variable
The condition of your while should be //while( (fscanf(fp, "%s %s", &nome, &senha)) != EOF ) { while ((fscanf(fp, "%s%s", nome, senha)) == 2) { The differences are use of & in the…
-
0
votes1
answer254
viewsA: Dynamic allocation and pointers in Lua
No need to pass a pointer copy to the allocation function the function aloc() can only return the pointer without receiving anything You forgot to define A and B in function main() Check the result…
-
1
votes2
answers582
viewsA: Doubt pointer cast
u_char variavel_teste struct teste *p ; p = (struct teste *)variavel_teste; The last line has an error and the compiler has to complain (error or Warning). What this expression does is take the…
-
0
votes1
answer438
viewsA: How to pass a pointer inside a struct per function parameter?
typedef struct {int **ponteiro;} Ponteiro; Ponteiro a; int b; int *c = &b; a.ponteiro = &c; fx(a.ponteiro); // &c : tipo int ** fx(*(a.ponteiro)); //…
-
1
votes1
answer118
viewsA: Optimization of Cache Memory usage
TLDR the code ... int is faster than short int; the Elements of an array are contiguous (you do not need to malloc()/free()).
-
-1
votes2
answers181
viewsA: Compiling C for raw binary
Uses gcc -S option: https://gcc.gnu.org/onlinedocs/gcc-2.95.2/gcc_2.html#SEC4 -S Stop after the Stage of Compilation Proper; do not Assemble. The output is in the form of an Assembler code file for…
-
2
votes1
answer3481
viewsA: Failure to read char with scanf ("%c")
Your problem comes from scanf() previous, for example printf ("\n\n Digite a quantidade de colunas da matriz: "); scanf ("%d",&c); What happens when the user chooses, for example, 8 columns is…
-
2
votes2
answers109
viewsA: Unix and linux are reserved words?
The gcc is a compiler for several languages. https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc/Standards.html#Standards By default, GCC provides some Extensions to the C language that on Rare occasions…
-
0
votes1
answer94
viewsA: How to read a charde a file and convert to whole
In instruction printf("%d", c); the char is automatically converted to int without having to do anything. It is part of the "integer promotions" rule (see 6.5.2.2p6 in Standard C11 (English))…