Posts by user72726 • 956 points
48 posts
-
0
votes2
answers635
viewsA: Vector with pointers ordered in ascending order in C
A simpler example using the function qsort of the standard library: #include <stdio.h> #include <stdlib.h> int compara(const void* a, const void* b) { return *(int*)a - *(int*)b; } int…
-
1
votes1
answer46
viewsA: Matrix in C language, error zsh: abort
The problem is that the names are not being read correctly. To read a string through the scanf you need to pass the address of the first character of the string. See a corrected example: //Leitura…
-
0
votes1
answer58
viewsA: Problem with DYNAMIC ALLOCATION with char pointer in structs
The problem is that the code snippet below allocates memory to only the pointer nome of the first vector element. ptr1->nome = (char*) malloc(sizeof(char) * tamanho_nome); The right would be to…
-
0
votes1
answer103
viewsA: How to place a two-dimensional matrix, without defined size in a struct in C, and access the elements of this matrix by the struct itself?
Because vectors are static, the compiler must know their size at compile time. If the size will only be set at runtime, the vector must be dynamically allocated. Example of a function that allocates…
-
1
votes2
answers194
viewsA: How to detect when the user enters something invalid or wants to terminate the program first?
Maybe what you need is something like: #include <algorithm> #include <iostream> int main() { int menor; int maior; std::cin >> menor; maior = menor; for(int i = 0; i < 19; i++)…
-
0
votes1
answer21
viewsA: What is the error in my pointer passing to the read_vector() function?
Variables should not be passed to function scanf by value. The correct form would be to pass the address of the variable. scanf_s(" %d", vet + i);
-
0
votes1
answer436
viewsQ: 3D Python Graphics
I have 3 arrays, from the arrays I must draw a 3D graphic. I have tried several ways but I could not. I believe I am not creating the data frames correctly. I’m trying to do it this way: x =…
-
0
votes1
answer80
viewsA: How to read only one line of the C file?
In your case, I think the easiest solution would be: int main(){ FILE *p; if((p=fopen("votos.txt","r"))== NULL){ printf("\n Nao foi possivel abrir o arquivo"); exit(0); } int val; fscanf(p, "%d",…
-
1
votes1
answer74
viewsA: Change class attributes passed as parameter to another class
In C++ the arguments are, by default, passed by value, i.e., the function that receives the argument has access to a copy of the variable and not the variable itself. The copy of the variable is…
-
0
votes2
answers75
viewsA: Error in Arduin (pointer to struct)
You did not correctly define the name Seringa. The correct way is: typedef struct { int pot[7]; float nivel[7] = {0, 0.5, 1, 1.5, 2, 2.5, 3}; } Seringa; Seringa *seringa; seringa =…
-
3
votes1
answer53
viewsQ: What’s wrong with using reinterpret_cast on C++?
I know what to use reinterpret_cast may cause indefinite behavior, but I still don’t understand why (I know it has something to do with the life cycle of the object and alignment of memory). I would…
-
1
votes1
answer45
viewsA: Assignment operator overload returns a C++ reference
I could not understand why the overload of the atibuição operator returns a reference. Not necessarily, the programmer’s choice. if I give a Return *this (to my understanding) it makes the process…
-
3
votes4
answers2153
viewsA: Function to invert a string
The problem is that your function returns a char but reverse_string is a vector, the correct would be to return char*. Also, you forgot to mark the end of the converted string with the null…
-
1
votes1
answer147
views -
3
votes1
answer553
viewsQ: Error trying to draw graph: "length of the larger object is not multiple of the length of the smaller object"
I have a function: my_gamma = function(x) { f = function(t){t^(x-1) * exp(-t)} integrate(f, 0, Inf) } I’m trying to draw your chart with ggplot2: plot = ggplot(data.frame(x = seq(0.01, 10)),…
-
0
votes2
answers199
viewsA: How to remove white space on a vector pointer?
You could do it like this: char c; int i = 0; while (1) { while((c = getchar()) == ' ') continue; if(c == '\n' || i == n) break; buffer[i] = c; i++; } The while internal skips any blank spaces…
-
0
votes1
answer29
views -
0
votes1
answer67
viewsA: List of states where each node will be linked to a binary city search tree
A solution would be: typedef struct celCidade apontadorCidade; typedef struct celEstado apontadorEstado; struct celEstado{ char nome[30]; int populacao; int beneficiarios; int qtdCidades; float idh;…
-
4
votes1
answer1129
viewsQ: Calculate derivative and integrals in R
I have a set of functions that I need to calculate their respective derivatives and integrals. Because there are many I thought it best to create a function that takes an expression as an argument…
-
1
votes2
answers380
viewsA: Return of system(); c++
Maybe what you need is the function fopen instead of system. You can do the following: FILE* output_file = nullptr; char buffer[1024]; output_file = popen("whoami", "r"); if(output_file) { int i =…
-
1
votes1
answer34
viewsQ: What is the difference between span and string_view in c++20?
The two classes do basically the same, have a pointer to the first element of the array and the size of the array. I know that string_view is used for character arrays, but could not be used span…
-
3
votes1
answer60
viewsQ: Why do you have to add -pthread option when compiling with the Std thread library?
If in the main.cpp I use the library thread of std, to compile I have to use the following command: g++ main.cpp -pthread And even applies to Ang. This is the only case I know of the standard…
-
1
votes2
answers128
views -
0
votes1
answer42
viewsA: Reading from a file
The problem is that the function read does not allocate memory to the buffer. This means that you first need to initialize the pointer buff with a valid address. Ex: char* buff = malloc(size);…
-
2
votes1
answer216
viewsA: Is it good practice to use virtual functions in non-derivative classes?
As you said yourself, using virtual keyword serves to indicate that the function can be overwritten in another part of the code. So, if you are using the class in a polyformic way, that is, if you…
-
2
votes1
answer1893
viewsA: Cryptography in C
You have two problems with your code. The first is overflow. A signed int (or simply int) stores values between -2,147,483,648 to 2,147,483,647. Now see what happens if the user wants to encrypt the…
-
0
votes2
answers47
viewsA: Vector inside the matrix?
Your logic is wrong. I have not tested the code but what its function does is to return the first line containing the last element of the vector. A good alternative would be to do the following:…
-
4
votes1
answer392
viewsA: Static int in class C++
Static data members are stored separately, as if they were not part of the object. So they must be declared outside their class. class teste { static int x; public: teste () { x++; } } t1; int…
-
0
votes1
answer52
viewsA: Segmentation fault na Function read_line
There should be no problem with your compiler. Your code problem is at the end of the function read_line: char *ret_line; memcpy(ret_line, line, strlen(line)); free(line); return ret_line; The…
-
0
votes1
answer1586
viewsA: How to remove a struct stored in c file?
A good way to do this would be to create a new file. Copy all contacts to the new file, except the one you want to delete. And in the end, remove the old file and rename the new. void…
-
0
votes1
answer68
viewsA: Can anyone tell me what’s wrong with this code?
The problem is in the gcValues variable. On line 84, before calling the Xcreategc function, you should initialize the foreground member variable. The right thing would be: gcValues.font =…
-
3
votes1
answer474
viewsQ: In C, can declaring variables in the middle of a block of code lead the program to undefined behavior?
I have read in several books that in C variables should be declared at the beginning of a code block. But what happens if I declare them in the middle? I was doing a program in c that shows a text…
-
2
votes1
answer68
viewsQ: Can anyone tell me what’s wrong with this code?
I am creating a function that receives and breaks a string into several, depending on the delimiter(s) chosen by the programmer. So I have: void split(const wchar_t* text, const wchar_t*…
-
1
votes0
answers32
viewsQ: Is there any (viable) way to develop Windows software on Linux? Any application for this?
I’m trying to create a cross-Plataform application (for academic reasons), which at least works on Windows and Linux. For development I can only use standard libraries of c/c++ and the native API of…
-
0
votes2
answers108
viewsA: Question about array size
Yes is perfectly normal. This is due to how variables are allocated in c, but also depends on the compiler. Observe the following situations: int a = 5, b = 10; int main() { int *p = &a;…
-
0
votes3
answers379
viewsA: How to insert a ";" at the end of the string, program in c
The function fgets returns all line characters, including the end-of-line character itself - \n (obviously, it is the last character of the string returned). And that is why the printing is not…
-
2
votes1
answer637
viewsA: Destamping Dynamic Stack
The whole problem is in the way you are "popping" the stack: aux = topo->prox; while(aux != NULL) { aux = aux->prox; topo->prox--; } Here are two points to highlight. It doesn’t seem to…
-
2
votes1
answer41
viewsA: Program in c++ hangs when I use a function to pick up a file name
The problem is in this line of code: memcpy (real_name,full_name+last_bar,strlen(full_name)); The function memcpy is copying extra bytes, ie the function is writing out of memory allocated by…
-
1
votes1
answer1119
views -
1
votes1
answer211
viewsA: Cast in void pointer
The error is because the compiler does not know how to treat the pointer info, since it does not point to a specific type of data. Then the solution would be to cast, for example: printf("Base.....:…
-
0
votes1
answer1550
viewsA: Add row and column values and store in vector
In function processaDadosno need to use the variable soma, everything can be done in a very simple way. For example: int i, j; for(i = 0; i < 5; i++){ vetorA[i] = 0; for(j = 0; j < 4; j++){…
-
0
votes1
answer79
viewsA: I’m not managing to sort the notes with the use of struct
The function commands that you write does nothing more than exchange the elements. The right thing would be to sort within the function. Ex: void ordena(double vet[]) { int i, j; double aux; for (i…
-
7
votes3
answers145
viewsQ: How to embed one library into the other?
I have created a C++ game development library. Only my library needs another to display the images on the screen, the SDL2. So every time someone wants to use my library, they would have to link to…
-
4
votes2
answers263
views -
1
votes1
answer83
viewsA: composition and validation
The problem is you declared the "getContact" function as "Contact" type but at the end of the function you did not return the value. Start watching the warnings!
-
1
votes1
answer288
viewsA: I have a question about inherited default builders
What happens is that you are not modifying the values of your class. Filho::Filho(int arg1, arg2){ if(arg2>0) Pai(arg2); //<- aqui está o erro, uma nova instancia da classe pai é criada else{…
-
0
votes1
answer1351
viewsA: Accessing matrix row and swap row by column
From what I understand of the problem the answer is quite simple and does not need the algorithm of Buble Sort. #define TAMANHO 4 #include <stdio.h> int main(){ int matriz[TAMANHO][TAMANHO];…
-
1
votes1
answer1569
viewsA: How to pass string as parameter in C++
You should do it in the c++ style, where you don’t need the size parameter, just call the size() method to get the string size. And in the end return the string after conversion. std::string…