\Device\NUL
Static memory cannot be reallocated?
stack memory once allocated can't be resized
Alessandro
Yes I mean, that’s probably the issue
%Nikita
You can reallocate static array by creating new one. This function reallocates static array: void* my_realloc(void* from, void* to, size_t sfrom, size_t sto){ memset(to, 0, sto); memcpy(to, from, sfrom); return to; } and you can use it like: int ages[] = {23, 43, 12, 89, 2}, ages2[10]; int *p = (int*)my_realloc(ages, ages2, sizeof(ages), sizeof(ages2)); printf(“copied array: “); for (int* k = p; k < p+10; k++) printf(“%i - “, *k);
%Nikita
Or if you don’t want to create many arrays just to reallocate some data, instead of using malloc or calloc you can create one huge array, and make your own allocate function to work with. That’s a variant too
Ster-Devs
Sadly stack memory is limited
😅 Depends of what you want to do
%Nikita
Sadly stack memory is limited
I know, but I often use this trick when I don't need so many elements, an array of 255..1024 elements will be enough
the best for everyone
#include<stdio.h> int main () { char cha[100]; int i=0,j,k; printf("Enter the words that you want to write /n"); gets(cha); for(i=0; cha[i]!='\0'; i++) // to denote string { k=1; // because it will be at least one characters for(j=i+1; cha[j]!='\0';j++) // to store repeated characters { cha[i]=cha[j]; // to store the new of them } k++; cha [j]= ' ' ; // to remove the repetition of characters of it } cha[j] = ' ' ; printf( "\n %c appcarred = %d times ",cha[i],k); }
the best for everyone
what this app is not working
Pavel
what this app is not working
By "not working" you mean crashing or giving not expected result, or something else?
Pavel
its running but its not working
What does "not working" mean?
the best for everyone
What does "not working" mean?
its not working as much as the purpose of the code
Hima
its not working as much as the purpose of the code
And what's the purpose of your code ?
the best for everyone
Hima
counting the characters
You have built yourself a very weird logic. for(j=i+1; cha[j]!='\0';j++) // to store repeated characters { cha[i]=cha[j]; // to store the new of them } What do you want to do in this inner loop?
JY
Hi anyone interested in joining a crypto project? C++, tcpip, grpc, database skills required
Hima
i wrote next to it what do i want from them
I don't get it, care to explain? Anyways , this can be a lot more simpler. int ch[26]={}; for(int i=0;cha[i];i++) ch[cha[i]-'a']++; for(int i=0;i<26;i++) { if(ch[i]) printf( "%c appeared = %d times ",cha[i],ch[i]); }
the best for everyone
how come that
the best for everyone
I don't get it, care to explain? Anyways , this can be a lot more simpler. int ch[26]={}; for(int i=0;cha[i];i++) ch[cha[i]-'a']++; for(int i=0;i<26;i++) { if(ch[i]) printf( "%c appeared = %d times ",cha[i],ch[i]); }
#include<stdio.h> int main () { int ch[26]={}; for(int i=0;cha[i];i++) ch[cha[i]-'a']++; for(int i=0;i<26;i++) { if(ch[i]) printf( "%c appeared = %d times ",cha[i],ch[i]); } }
Shubh
i am using vs cod but i am input 2 values and skip the program:
Shubh
#include <stdio.h> int main () { int first,last,i,j; printf ("Enter a first number: "); scanf ("%d",&first); printf ("Enter a second number: "); scanf ("%d",&last); for (i=first ; i<=last ; i++) { for (j=2;j<=i;j++) { if (i%j==0) break; } if (i==j) printf ("%d\n",j); } return 0; }
Shubh
PS E:\language> cd "e:\language\c\c++\" ; if ($?) { gcc prime_numer_rang.c -o prime_numer_rang } ; if ($?) { .\prime_numer_rang } Enter your first number: 10 Enter your second number: 15 PS E:\language\c\c++>
Shubh
please help me
as
please help me
your code is working good
Shubh
it's working thankyou
Anonymous
https://i.imgur.com/B9DMnlS.png Can someone explain these three differences between classes and structures in C++?
Null
Do anyone use gets() fxn in c or c++
Talula
Do anyone use gets() fxn in c or c++
Yes, some people do, specially if you're getting input from IO device which isn't your keyboard, like terminal or serial, etc.
Null
Oh ohkk
Null
#include<stdio.h> #include<string.h> main() { int wc,cd,diets,cost,et,price,l,total; char a[20]; /* printf(" *** MESS BILL ***\n"); printf("\n Enter the worker charges : Rs."); scanf("%d",&wc); printf("\n Enter the number of compulsory diets:"); scanf("%d",&cd); printf("\n Enter the number of diets taken :"); scanf("%d",&diets); printf("\n Enter the cost per diet : Rs."); scanf("%d",&cost); */ printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :"); scanf("%d",&l); printf("\n Enter the name of any extra thing taken :"); gets(a); printf(a); printf("\n Enter the amount :"); scanf("%d",&et); printf ("\n Enter the price of %s : Rs.",a); scanf("%d",&price); if(diets>cd) total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price); else total=wc+(cd*cost)+(l*20)+(et*price); printf("\n TOTAL BILL=%d",total); return 0; }
Null
Please help me out here why isn't this working
\Device\NUL
Do anyone use gets() fxn in c or c++
gets() is no longer available since C11. And it's deprecated
Null
then how should we enter a string in c
\Device\NUL
then how should we enter a string in c
scanf with max field width or fgets from stdin
I use Arch
then how should we enter a string in c
https://lmgtfy.app/?q=user+input+c
Null
scanf with max field width or fgets from stdin
I tried with fgets to its still isn't working
Null
scanf with max field width or fgets from stdin
With scanf it is not considering white spaces
\Device\NUL
With scanf it is not considering white spaces
Because your input stream isn't clear
\Device\NUL
There's still newline in the input stream
Null
Wdym🤔?
\Device\NUL
Wdym🤔?
scanf(" ", ...) scanf(" ", ...) Did you know why before you input the second scanf it receives input ?
\Device\NUL
Because scanf leave newline in the input stream, and the newline goes to next input
\Device\NUL
You need to clear the input stream
Talula
#include<stdio.h> #include<string.h> main() { int wc,cd,diets,cost,et,price,l,total; char a[20]; /* printf(" *** MESS BILL ***\n"); printf("\n Enter the worker charges : Rs."); scanf("%d",&wc); printf("\n Enter the number of compulsory diets:"); scanf("%d",&cd); printf("\n Enter the number of diets taken :"); scanf("%d",&diets); printf("\n Enter the cost per diet : Rs."); scanf("%d",&cost); */ printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :"); scanf("%d",&l); printf("\n Enter the name of any extra thing taken :"); gets(a); printf(a); printf("\n Enter the amount :"); scanf("%d",&et); printf ("\n Enter the price of %s : Rs.",a); scanf("%d",&price); if(diets>cd) total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price); else total=wc+(cd*cost)+(l*20)+(et*price); printf("\n TOTAL BILL=%d",total); return 0; }
#include<stdio.h> #include<string.h> int main() { int wc,cd,diets,cost,et,price,l,total; char a[20]; /* printf(" *** MESS BILL ***\n"); printf("\n Enter the worker charges : Rs."); scanf("%d",&wc); printf("\n Enter the number of compulsory diets:"); scanf("%d",&cd); printf("\n Enter the number of diets taken :"); scanf("%d",&diets); printf("\n Enter the cost per diet : Rs."); scanf("%d",&cost); */ printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :"); scanf("%d",&l); printf("\n Enter the name of any extra thing taken :"); scanf("%s",a); printf("%s\r\n",a); printf("\n Enter the amount :"); scanf("%d",&et); printf ("\n Enter the price of %s : Rs.",a); scanf("%d",&price); if(diets>cd) total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price); else total=wc+(cd*cost)+(l*20)+(et*price); printf("\n TOTAL BILL=%d",total); return 0; }
\Device\NUL
Do it will work jusr one time like it would take single input??
It will take only one input because the remained newline goes to next input
Null
Ohkk 🤔
\Device\NUL
But it won't take blank spaces
scanf("%fieldwidth[^\n]", str);
Null
But there is no scan in my code before that one
Talula
Null
You can use fgets().
Tried didn't work
\Device\NUL
Tried didn't work
I told you there's still remained newline in the input stream
Null
Yea but that is when we use scaning statement before that 🤔 if i am right??
\Device\NUL
*Confused nick young
Null
Yea i am confused 😅
Talula
Yea i am confused 😅
#include<stdio.h> #include<string.h> int main() { int wc,cd,diets,cost,et,price,l,total; char a[20]; /* printf(" *** MESS BILL ***\n"); printf("\n Enter the worker charges : Rs."); scanf("%d",&wc); printf("\n Enter the number of compulsory diets:"); scanf("%d",&cd); printf("\n Enter the number of diets taken :"); scanf("%d",&diets); printf("\n Enter the cost per diet : Rs."); scanf("%d",&cost); */ printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :"); scanf("%d",&l); getc(stdin); printf("\n Enter the name of any extra thing taken :"); fgets(a,sizeof(a),stdin); printf("%s\r\n",a); printf("\n Enter the amount :"); scanf("%d",&et); printf ("\n Enter the price of %s : Rs.",a); scanf("%d",&price); if(diets>cd) total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price); else total=wc+(cd*cost)+(l*20)+(et*price); printf("\n TOTAL BILL=%d",total); return 0; }
Talula
This should work...
Null
Why you put getc(Stdin)?
Talula
Because when you press enter '\n' remains in the buffer, when you put getc(stdin) it takes that char and does nothing.
Talula
Basically clearing the buffer for stdin
Anonymous
Thanks For Warm Welcoming 🌹Rose!
Null
Thanks alot @Tazmikar and @kawaiighost
\Device\NUL
\Device\NUL
Another way to store string without getting newline from stdin (get string using fgets) is using scanf with max field width Ex: #define MAX 100 /* one more bytes for NULL char */ char str[MAX + 1]; scanf("%100[^\n]", str); getchar();
Null
[redacted]
Ok thank you 😊
the best for everyone
// Two dimensional array with a function and how to sum them togother and get the average of them and the maximum average and sum only the students #include<stdio.h> void TwoDimensionalArray(int subjects [2][5]) { int i=0,j=10,sumStudents=0,allSum=0,average=0,maxSubject=0; for(i=0;i<2;i++) { printf("student number is %d ",i); for(j=0;j<5,j++) { printf("student number is %d and subjects number is %d ",subjects[i][j] ); allSum+=subjects[i][j]; sumStudents+= subjects[i][j] ; average= sumStudents /subjects[i][j]; maxSubject = ; } printf("the sum of the students is %d ",sumStudents); sumStudents=0;// because he wants to sum students one by one } printf("the whole sum of subjects is %d",allSum); printf("\n"); } int main() { printf("Hello wold"); int marks[][5]={ {10,20,30,40,50} , {40,50,80,10,30} }; marks[0][0] = 10; marks[0][1] = 10; marks[0][2] = 10; marks[0][3] = 10; marks[0][4] = 10; for(int i=0;i<2;i++) { for(int j=0;j<5;j++) { printf("Please Enter the mark of the student number"); scanf("%d "marks[i][j]); } } int TwoDimensionalArray(marks,[2][5]); // passing by reference }
the best for everyone
i have a problem in this code
the best for everyone
can anyone help me to know about the problem of it