Posts by pmg • 6,456 points
254 posts
-
2
votes1
answer228
viewsA: Assign element to matrix only when there is space?
You have to check if the space is available before you write the values. You also need to check that the random position does not leave the existing space. Suppose you wanted to enter 5 values…
-
0
votes1
answer67
viewsA: Matrix with dynamic columns?
Put zeroes on the left // 1234 numeros[0][0] = 0; numeros[0][1] = 0; numeros[0][2] = 1; numeros[0][3] = 2; numeros[0][4] = 3; numeros[0][5] = 4; // 14086 numeros[1][0] = 0; numeros[1][1] = 1;…
-
8
votes2
answers193
views -
14
votes6
answers2425
views -
6
votes1
answer2462
viewsA: Get size from a character array?
The definition char nome[10] = {'b', 'r', 'a', 's', 'i', 'l'}; is equal to char nome[10] = {'b', 'r', 'a', 's', 'i', 'l', 0, 0, 0, 0}; so you can use the function strlen() #include <stdio.h>…
-
4
votes1
answer58
viewsA: Take element size from a vector?
So suddenly I see two ways: converting to string or using the function log(). #include <string.h> // para strlen() int nums[] = {1235, 14627, 1625161, 54437}; for (int k = 0; k < sizeof…
-
6
votes2
answers815
viewsA: Why declare pointer to array if arrays are already pointers?
An array char (or other type) IS NOT a pointer. An array is an array; a pointer is a pointer (see Section 6 of c-Faq). When an array is used as value, it is converted to a pointer to its first…
-
0
votes2
answers594
viewsA: Application of Bubble Sort
My compiler gives warnings in several lines: pt91088.c:5:34: warning: data argument not used by format string [-Wformat-extra-args] printf("Informe valor:", vetor[i]); ~~~~~~~~~~~~~~~~ ^…
-
1
votes2
answers313
viewsA: How to access the pointer of a struct within the pointer of another struct?
Vertice within the struct grafo has kind int *. These two types are not compatible. When you index a type value int * get a int and a type value int has no members. Defines the struct grafo as…
-
2
votes1
answer186
viewsA: Program compiles but stops responding during execution
int main() { PGM *imgconv; char kernel[3][3]={{-1, 0, 1},{-1, 0, 1},{-1, 0, 1}}; if(SalvarPGM(Convolucao(LerPGM("entrada.pgm"), kernel, imgconv), "saida.pgm")) printf("Operacao realizada com…
-
8
votes1
answer1150
viewsA: Recursive analysis of a vector
Your conditions are incomplete. An array of N elements is composed of increasing numbers when the smallest array with the first ones N - 1 is composed of increasing numbers and the penultimate…
-
1
votes4
answers658
viewsA: How to test the condition on a vector?
Your biggest problem is using "characters" with more than one symbol. ' text[i]' has 8 symbols; ' *' has 2 symbols. Each character should be only 1 symbol. if (' ' == text[i]) /* ... */; text[i] =…
-
4
votes2
answers1190
viewsA: How to perform an algebraic expression on a string in C
If you are not limited to the basic standard and can use POSIX (which describes the function popen()), then passes the string to an external calculator, for example bc. #include <stdio.h>…
-
3
votes2
answers339
viewsA: String reading and input buffer
Notice that not all input is done with fgets(). The last part, of the participant’s occupation, is made with scanf(). Don’t mix fgets() with scanf(). If necessary uses sscanf(). char tmp[12];…
-
2
votes3
answers1286
viewsA: while for number other than 0 in C
Are you reading to consumidor[i].codTC but to verify consumidor[i].num. scanf("%i", &consumidor[i].codTC); // ... while(consumidor[i].num != 0) You only read to consumidor[i].num when the first…
-
4
votes3
answers6933
viewsA: Rotate matrix by 90º
To change the value of 2 variables it is usual to use a temporary variable. // troca a e b int tmp = a; a = b; b = tmp; In case you run the matrix, you have to do the same but with 4 variables //…
-
2
votes2
answers7055
viewsA: What is the purpose of the sizeof command?
Whenever you need to know the size of an object (or the number of sub-objects) you should use the sizeof. For example, to copy an array to an allocated memory zone int arr[52]; p = malloc(sizeof…
-
4
votes2
answers7902
viewsA: How to define the vector size dynamically in C?
You basically have two forms: use of malloc() and friends use of VLA, from C99 Using malloc() and friends you can change the size of the array whenever you need it; using VLA size is fixed after…
-
5
votes4
answers456
viewsA: Memory allocation for pointers
The book in question misleads. Pointers are objects that need storage space such as "normal type objects".
-
10
votes1
answer882
viewsA: comparison of floats
double numero1, numero2, numero3; scanf("%f %f %f", &numero1, &numero2, &numero3); Error: the specification "%f" of scanf() waits for a pointer to a variable of the type float, but…
-
0
votes1
answer79
viewsA: Error in . / executable
After the first realloc() campo = (char**) realloc(campo, (i + 1) * sizeof(char*)); the new pointers in campo have an unspecified value. In particular the first time this instruction is executed…
-
1
votes1
answer298
viewsA: Copying from a file to an array
ponteiro = fopen("nome", "w"); <== this will open the file for writing (wRite) and empty it. You must do ponteiro = fopen("nome", "r"); The use while(!feof(ponteiro)) is incorrect. The function…
-
0
votes4
answers5886
viewsA: Language exercise C
A recursive solution :-) #include <stdio.h> #include <stdlib.h> void le_imprime(int n); int main(void) { puts("Digite 6 numeros inteiros."); le_imprime(6); puts(""); return 0; } void…
-
5
votes2
answers111
viewsA: Access to specific memory points
Your operating system will not allow you to access memory that does not belong to your program. #include <stdio.h> int main(void) { unsigned char *mem = 0xdeadbeef; // ou = 3735928559;…
-
4
votes2
answers661
viewsA: Where to create macros in C?
First point: avoids the use of macros as functions, makes real functions. Second point: in general macros should be created in header files as they define identifiers to be used by external…
-
2
votes2
answers189
views -
3
votes4
answers29520
viewsA: Print strings in alphabetical order
The strcmp() returns one of three hypotheses. cmp = strcmp(n1, n2); if (cmp == 0) /* n1 igual a n2 */; if (cmp < 0) /* n1 menor que n2 */; if (cmp > 0) /* n1 maior que n2 */; As long as you…
-
1
votes2
answers290
viewsA: How to convert string array to string?
leiaS defined as a function (of type String (*)(void)) String leiaS(void) { /* ... */ } The reference to leiaS on the line of digitalWrite digitalWrite(leia(), inverte(leiaS))); is like a parameter…
-
2
votes2
answers1237
viewsA: How can I get more accurate values by dividing two long?
(double)x/(double)m; //resulta em 0.01 (double)y/(double)m; //resulta em 0.00 (double)z/(double)m; //resulta em 1.00 99/9999 = 0.00990099(0099...) 9/9999 = 0.00090009(0009...) Maybe your problem is…
-
3
votes1
answer743
viewsA: Socket TCP in C
You gotta put the recv() within a cycle until it returns 0. char buffer[40000]; int bsize = 40000; int blen = 0; while (blen < bsize) { int bytes = recv(socket, buffer + blen, bsize - blen, 0);…
-
1
votes1
answer206
viewsA: Multiway Mergesort-External errors in C
You got a mistake here int varTemp; // tipo de variavel ar = arqmanip->arquivos[inser]; fscanf(ar, "%ld", &varTemp); // e especificacao de scanf // incompativeis The variable verTemp has kind…
-
0
votes2
answers400
viewsA: Reading an array of a file
while(fscanf(ponteiro, "%d", &aux) != '\n') { counterC++; if(fscanf(ponteiro, "%d", &aux) == '\n') counterL++; } This is an infinite cycle. The value returned by the first fscanf() NEVER…
-
2
votes1
answer111
viewsA: how to force a buffer to behave like a string in C?
Adds p '\0' buffer. But attention to binary data!! length = recv(socket, buffer, sizeof buffer - 1, 0); buffer[length] = 0; // adiciona terminador
-
0
votes3
answers557
viewsA: Convert "unsigned int" to "unsigned char" vector?
Basically something like this unsigned int valor = 1042; unsigned char vetor[38]; // escolher limite apropriado int len = snprintf(vetor, sizeof vetor, "%u", valor); // vetor 'e a string "1042", ou…
-
1
votes2
answers7781
viewsA: Generate random number without repetition in C
Put all possible numbers into an array; shuffle that array; then use the required values. int valores[] = {4, 5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19, 24, 25, 26, 27, 28, 29}; // 18 valores, para 16…
-
0
votes2
answers192
viewsA: File manipulation
Compile with as many warnings and errors as your compiler allows. Within the function main() you have case 1 : system("cls"); insere(alg, arquivo); without an active prototype for function insere().…
-
3
votes2
answers2082
viewsA: Generate random numbers from a predefined set
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { srand(time(0)); // escolhe sequencia de numeros aleatorios int valores[] = {3, 10, 20, 2334}; int n =…
-
1
votes1
answer92
viewsA: Remove http header in socket C
Do not start writing the final file before the header is finished int headerterminado = 0; char *p = NULL; while (recv(socket, buffer, ...)) { p = strstr(buffer, "\r\n\r\n"); if (p) headerterminado…
-
2
votes2
answers385
views -
5
votes1
answer1562
viewsA: How to make a wrong user input not bug the program?
Checks the amount returned by scanf(): if (scanf("%d", &opc) != 1) /* erro */ Even better to use fgets() followed by sscanf(): char tmp[20]; if (!fgets(tmp, sizeof tmp, stdin)) /* erro */; if…
-
2
votes3
answers187
viewsA: Return to condition start when prompted
for (;;) { /* codigo */ } or while (1) { /* codigo */ } or do { /* codigo */ } while (1):
-
6
votes3
answers3929
viewsA: Limit the size of a String?
You need room for the '\0' and you can’t put the & operator scanf("%9s", pl1); // p1 = "dinamicam\x00" but the rest of the string stays in the buffer // ente<ENTER> and will be caught in…
-
1
votes2
answers113
viewsA: Calling function without signature knowledge
Tip: Show your code. I’ll show you mine :-) #include <stdio.h> #include <string.h> typedef int (*fx_t)(int); /* fx_t e ponteiro para funcao que recebe e devolve int */ int fx1(int a) {…
-
0
votes1
answer79
viewsA: Error Segmentation fault
char* diminuieretira(char** p, int r) { int i; for(i = 0; i < r; i++) { if(isupper(*p[i])) tolower(*p[i]); else if((*p[i]) == 46 || (*p[i]) == 44 || (*p[i]) == 59 || (*p[i]) == 58 || (*p[i]) ==…
-
1
votes1
answer98
viewsA: How to check the elements close to a specific element in a matrix?
If you put the campus in a matrix, the most direct method (brute force) works well char campus[MAXROWS][MAXCOLS]; // input, preenche campus, obtem nrows e ncols // para todos os elementos que nao…
-
1
votes1
answer147
viewsA: How to read the last 3 characters of a char with no set size
You have miscalculation memcpy(valor_final, string_total[strlen(string_total - 3)], 3); // v v ^ | memcpy(valor_final, @string_total[strlen(string_total) - 3], 4); Don’t forget to copy the…
-
1
votes1
answer1157
viewsA: Aid on a family tree in C
String comparison cannot be done with < or >. You have to use strcmp() (prototype in <string.h>) // if (Id < Raiz->Id) if (strcmp(Id, Raiz->Id) < 0) /* ... */; or // if (Id…
-
1
votes2
answers358
viewsA: Doubt about compilation in C/C++
The compilation of functions that have prototypes, for example, in <stdio.h> was made by someone else on another computer. This person put these compiled objects into a library and when you…
-
1
votes1
answer1280
viewsA: problems with 'Multiple Definition of...' in files. The
Do not include files with extension . c! Puts the definition of structures in files . h (a struct _voo and the struct _aeroporto). Put function prototypes in files . h. Puts the include Guards in…
-
0
votes3
answers2396
viewsA: How to read the first three characters of a string?
If you set the array that will save the read string of the file with space to only 3 characters plus the terminator, the function fgets() does so automagically: char principio[4]; //char…