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( , " ").
Anonymous
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?
Anonymous
Anonymous
@𝑺𝒐𝒃𝒌𝒂
Anonymous
Amol
Anonymous
Amol
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
Anonymous
Amol
Anonymous
Anonymous
or an array of struct bill (bill number is the index)
Anonymous
Anonymous
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
z
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
Anonymous
Hanz
Hanz
i want to cross-compile
Anonymous
z
Hanz
Hanz
Hanz
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
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?
I can't reproduce it. It returns 55 from my end.
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
Артем
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
Abhishek
Can someone suggest some sites for preparing for cpp output question
Abhishek
Interview questions
Mr.
ㅤㅤㅤ
How can we input an Array without any loops
klimi
klimi
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)
klimi
ㅤㅤㅤ
any value by user
klimi
then, you could directly copy stdin to the memory
Nils
🗽
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
Surya
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;
}
Fatih
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
Fatih