Amol
use C++ style casts instead of C style casts and the answer is obvious 2. const_cast<char *>(s.c_str());
#include<iostream> #include<cstring> using namespace std; string extractStringAtKey(string str,int key){ char* s=strtok((char*)str.c_str()," "); while(key>1){ s=strtok(NULL," "); key--; } return (string)s; } int main(){ string s("10 20 30 40"); int key; cin>>key; cout<<extractStringAtKey(s,key); } Above program is working fine.But i have a doubt why cant we simply use str.c_str() instead of (char*)str.c_str() in the function strtok( , " ").
Good
In the project(cpp) I am working we have large number of numeric values for each class.So I made it as const variables,My doubt is is it ok to declare the class specific variables inside the class or outside class declaration(in *.h file).Its not exactly problem to post but what is the good practice to follow?
@𝑺𝒐𝒃𝒌𝒂
If is_open failed then you should not continue with the rest of the operations. If you do, you will only see garbage values (undefined behavior) You should do something like this if(!inFile.is_open()){ cerr << "File opening for read failed"; exit(1); //or throw an exception } else { //rest of your code }
It work 😊. Thx very much for helping 🤝. Moreover I had to change working directory in project-> properties too. https://stackoverflow.com/questions/16642517/why-cant-i-read-a-file-with-codeblocks-c/17016256
Anonymous
This, u trying to tell something about pointer to const,, and pointer to non-const
c_str() returns a const char *, using that pointer to modify the string is undefined behaviour.
Prince
Help me please; Qsn. A programmer created a programme for a hospital bill to store patient details as a single entity consisting of:PatientNo, Patient_Name,Gender and Age. i: identify the most appropriate data structure the programmer could have used. ii:using C language,declare a data structure that could be used to store this data
Prince
show the code you have done and where you are stuck
First am confused which data structure am going to use,,if only I understand that data structure am able to do the rest
Anonymous
use the data() member function if you want a pointer to non-const
For what he is doing, using data() (the non const version) will also result in Undefined Behavior
Anonymous
or an array of struct bill (bill number is the index)
Anonymous
For what he is doing, using data() (the non const version) will also result in Undefined Behavior
only if the string gets reallocated in between the strtok() calls, right?
Anonymous
#include<iostream> #include<cstring> using namespace std; string extractStringAtKey(string str,int key){ char* s=strtok((char*)str.c_str()," "); while(key>1){ s=strtok(NULL," "); key--; } return (string)s; } int main(){ string s("10 20 30 40"); int key; cin>>key; cout<<extractStringAtKey(s,key); } Above program is working fine.But i have a doubt why cant we simply use str.c_str() instead of (char*)str.c_str() in the function strtok( , " ").
the return (string)s; can be dangerous (though in this case it just uses static_cast<string>(s)). please stop using C style casts. available alternatives - C++ temporary constructions - type(A) (instead of (type)A) C++ casts - static_cast<type>(A), const_cast<type>(A), dynamic_cast<type>(A), reinterpret_cast<type>(A) (you will do fine by learning only about the first two)
Prince
#include<stdio.h> #include<string.h> struct bill { char patient_name[2]; int age; int patientNo; char gender; } int main(){ struct patient_name; strcpy(patient name,""); patientNo=; gender=; age=; printf("name=%s\n",patient_name); printf("patientNo=%s\n",patientNo); printf("age=%d\n",age); printf("gender=%c\n",gender); return 0; }
Prince
Still confusing am stuck
Samuel
/get ide
Hanz
#ASK I have a project with some header and source files, under 3-5. It is still convenient to include them (.c source files) in the compile command, but what if i have 20 header and its source files? how can i handle such situation?
Hanz
i think i ever found a/some flag for this but i dont remember
z
#ASK I have a project with some header and source files, under 3-5. It is still convenient to include them (.c source files) in the compile command, but what if i have 20 header and its source files? how can i handle such situation?
You can separate the compilation unit with -c flag, after you got all sources compiled, you can link them together into single binary. So for example, you have a.c, b.c, and c.c. gcc -c a.c -o a.o gcc -c b.c -o b.o gcc -c c.c -o c.o gcc a.o b.o c.o -o bin
Hanz
i want to cross-compile
z
this is actually redundant, i can just do gcc main.c a.c b.c c.c -o bin I need something that can search for the x.c of x.h in a directory, is that actually possible?
Yes you can do it in single command, but in presence of multiple files, it can slow down your development, because when you make a change to a single file, you have to compile everything from zero + link it again. If you separate the compilation, you only need to recompile changed files and link it.
z
this is actually redundant, i can just do gcc main.c a.c b.c c.c -o bin I need something that can search for the x.c of x.h in a directory, is that actually possible?
> I need something that can search for the x.c of x.h in a directory, is that actually possible? Do you mean include path? You may be looking for -I flag.
Hanz
use CMake https://blog.feabhas.com/2021/07/cmake-part-1-the-dark-arts/
i never use cmake before, but ok i will try to understand it
z
i want to cross-compile
What is the deal with cross compiling? I don't see the relevance with your question.
Hanz
i will take CMake then, or use something like find includes | tr '\n' ' '
Артем
Hello, got an interesting case I`ve got uint8_t v1 and int8_t v2 variables uint8_t res Then I do following v1 = 75 v2 = -20 And after res = v1 + v2 //this gave me res equal to 55 as expected BUT while using macros, like this one MIN(a, b) ((a) > (b) ? (b) : (a)) MAX(a, b) ((a) > (b) ? (a) : (b)) LIMIT(a, min, max) (MIN(MAX((a), (min)), (max))) res = LIMIT(v1 + v2 , 0, 100) // this one returns me 100 to res Does C have some strange types cast in macros?
z
#include <stdio.h> #include <stdint.h> #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define LIMIT(a, min, max) (MIN(MAX((a), (min)), (max))) int main(void) { int res; uint8_t v1 = 75; int8_t v2 = -20; res = LIMIT(v1 + v2 , 0, 100); printf("res = %d\n", res); return 0; }
Артем
even with using limit macro?
z
even with using limit macro?
Care to show the minimal working example of your case?
Артем
I`ve captured the rootcause variable, named as signed was declared as unsigned char That legacy.. anyway, thanks a lot
z
Alright.
Mr.
Hello guys
Mr.
From Brazil here
Mr.
I want to know if anyone in this group already had worked with C++ in embedded systems
klimi
I want to know if anyone in this group already had worked with C++ in embedded systems
it might be better to ask you question instead asking if someone has worked with x, there are 14k members so it's highly probable (+ i think i have seen this topic here :) )
Abhishek
Can someone suggest some sites for preparing for cpp output question
Abhishek
Interview questions
ㅤㅤㅤ
How can we input an Array without any loops
Nils
Why has my question been deleted?
Nils
I am 100% sure it did not break rules
klimi
Why has my question been deleted?
so sorry, i was deleting messages and i didn't catch that someone has posted a message meanwhile i was deleting them, sorry @tuxifan 's Question: does rlimit RLIMIT_AS limit space allocated by brk() too?
Nils
Oh np
klimi
Oh np
(thanks for noticing btw, it was completely my fault)
ㅤㅤㅤ
input all bytes at once
Can you elaborate suppose I want to input 10 int in a[10]
z
so sorry, i was deleting messages and i didn't catch that someone has posted a message meanwhile i was deleting them, sorry @tuxifan 's Question: does rlimit RLIMIT_AS limit space allocated by brk() too?
Yes, according to man 2 getrlimit. RLIMIT_AS This is the maximum size of the process's virtual memory (address space). The limit is specified in bytes, and is rounded down to the system page size. This limit affects calls to brk(2), mmap(2), and mremap(2), which fail with the error ENOMEM upon exceeding this limit.
ㅤㅤㅤ
any value by user
klimi
then, you could directly copy stdin to the memory
🗽
Hi. i have written this program and i want to add these two matrixes together by using a function👇🏻 void vectorsum(int n, int *a,int *b,int *c)...... I dont know how to start that could u help me? int main(void) { int A[3][3]={{1,2,3},{4,5,6},{7,8,9}}; int B[3][3] ={{1,4,7},{2,5,8},{3,6,9}};‌‌ print_matrix(3,3,A); printf("\n\n"); print_matrix(3,3,B); return 1; } void print_matrix(int m,int n,int A[][n]) { int i, j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%3d",A[i][j]); printf("\n"); } }
Anonymous
Who knows arrays well in c
Anonymous
Can you help me
Anonymous
only if the string gets reallocated in between the strtok() calls, right?
No it wont be Undefined Behavior. I thought strtok will set the terminating null character to null again which would be Undefined Behavior but the C++17 standard specifically mentions that this is ok.
Fatih
#include <stdio.h> int main(int argc, char *argv[]) { int n=0; int summe=0; printf("Eingabe der Variable n \n\n"); scanf("n= %i \n",&n); summe=n*(n+1)/2; printf("Die summe= %i \n",summe); printf("Press enter to continue ..."); fflush(stdin); getchar(); return 0; }
klimi
klimi
Does the compiler complain?
Fatih
yes but the answer is everytime 0
klimi
I see... I am not sure but isn't %d supposed to be for digit?
klimi
In the scanf