\Device\NUL
ive replaced scanf with getchar
I only suggest this when you want input single char
\Device\NUL
ch = getchar() is better than scanf("%c", &ch)
Baki
hey man , thank you it is not exiting like before
Baki
but it is exiting before the for loop , should i send you the whole code in personal? because here there is limit to what i send
\Device\NUL
Idk, but try to fix your code until it compiles without no warn
Baki
in these after printf("Your flight has been confirmed! \n Here are the Flight details\n"); it is exiting
Baki
Idk, but try to fix your code until it compiles without no warn
ok but if you find any error in my code then please let me know
Baki
Have you add getchar() after all scanf() function ?
do i have to add getchar after the scanf function which is used for getting the integer input?
\Device\NUL
scanf() function will remain newline in input stream
Baki
Every scanf() function
ok, i did this , and still it is exiting after printf("Your flight has been confirmed! \n Here are the Flight details\n");
Baki
https://pastebin.com/DgNpDQ3B
Baki
Fix indentation
please help sir
Baki
so far i have not found anything
Baki
https://pastebin.com/DgNpDQ3B
when i debug it, its showing Program received signal SIGSEGV, Segmentation fault. __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:96 96
Baki
https://pastebin.com/DgNpDQ3B
i found out that passport array is causing the problem
Anshul
Why this prints 2 and not 0 Cout<<((2/sqrt(9))*(sqrt(9)));
Baki
how do i erase particular element from an array?
Anonymous
why should it print 0?
He means that 2 and 3 are integer values and 2/3 should be zero
\Device\NUL
std::cout is just like printf but without type specifier explicitly. It has type overload
Anshul
why should it print 0?
I searched online now Previously I was expecting it to be zero because two divided by 3 will be zero Now I understood that even 2 will be converted to double so the answer of 2/sqrt9 will be in double
Baki
hey, i want to assign null/0 to the elements of array how should i do it
conko
using namespace std is not a good practice. BTW, though unrelated to this topic because using directive is different from using declaration, std::cout and cout may differ after using std::cout due to ADL.
conko
hey, i want to assign null/0 to the elements of array how should i do it
You can use std::fill in <algorithms> for any containers and arrays, if you're using built-in arrays, std::memset in <cstring> is an another option with efficiency consideration.
Pavel
Using namespace std; No need std::
https://www.google.com/amp/s/www.geeksforgeeks.org/using-namespace-std-considered-bad-practice/amp/
conko
how do i erase particular element from an array?
It's impossible to shrink the size of an array, so you can't erase element in an array, if you want to do this, use variable length containers like std::vector and then use their erase_if method instead. However, there is a workaround for arrays. Those who like functional programming styles may tend to create new sequences instead of modifying the old one. For example, your_array | std::views::filter(pred) will create a new range containing all elements in your_array satisfying the predicate you offer. You can also use the old school std::remove_if and then use std::span or something to create a new non-owning wrapper.
\Device\NUL
You can use std::fill in <algorithms> for any containers and arrays, if you're using built-in arrays, std::memset in <cstring> is an another option with efficiency consideration.
If it's stack array like type arr[x] , just initialized the first member with zero, The rest also will implicityy initialized to zero
Jerry
im trying to write in a array but what i get is trash at the end not what i want
Jerry
https://www.toptal.com/developers/hastebin/raw/vakepicasi
Jerry
this is the code
Anonymous
Hello how to passing array of struct to a function?
\Device\NUL
Anonymous
pointer of struct parameter
can you give an example?
Hussein
there few ways to do that
Hussein
if you want to change the content of a structure you should use a pointer
Hussein
if you want to change the content of a structure you should use a pointer
if changing the original value isn’t necesary you can pass it as parameter
Hussein
He said array of struct, not struct
yes a pointer to an array of structures
Anonymous
if changing the original value isn’t necesary you can pass it as parameter
I just want to take the value to put in the function
\Device\NUL
I just want to take the value to put in the function
pass member of struct array as argument ?
Hussein
‘ void function( struct mytype * mystruct ) { // access it with *(mystruct + (sizeof(struct mytype) * index ) } ‘ this how to do a pointer
Anonymous
Hussein
‘ void function( struct mytype mystruct[] ) { // access it with mystruct[ index ] } ‘ this how to pass it as a parameter
\Device\NUL
Except you're doing it with another struct types that has different size
Anonymous
For example, the struct member is barang[1].nama, I want to put barang[1].nama into the function
Hussein
‘ void function( struct mytype * mystruct ) { // access it with *(mystruct + (sizeof(struct mytype) * index ) } ‘ this how to do a pointer
you define a struct type struct mytype{ int value1; int value2; } then create an array out of it struct mytype mystruct[30]; then pass a pointer to the first element to your function now you can access any element by doing pointer arithmatic as follows: *( mystruct + (sizeof(struct mytype * index)
conko
can you give an example?
Let's say int a[10], you can use std::fill(std::begin(a), std::end(a), 0) to assign 0 to every element in array a. Since non-member begin/end function is introduced in C++11, you can use std::fill(a, a+10, 0) instead if you are using older implementations.
conko
memset version is like: std::memset(a, 0, sizeof a)
Hussein
I don't think you need sizeof(mystruct) * index), why not + index directly
because the size of a structure is usually more that one byte
conko
but please note that the memset version is valid for specific values like 0, other values are possibly invalid, std::fill works for any values you want to assign.
\Device\NUL
int8_t x[] *(x + 1) with int32_t x[] *(x + 1) is different
\Device\NUL
Since 32 bit is 4 byte, it should be *(x + 4)
\Device\NUL
Edited
Hussein
‘ void function( struct mytype mystruct[] ) { // access it with mystruct[ index ] } ‘ this how to pass it as a parameter
remember that usually using this with array of structures isn’t a good idea because it will eatup more ram
Anonymous
ok thanks everyone i will try it
Anonymous
Approximately how long does it take to become proficient in using C language?
Hussein
understanding the basics only takes few weaks but getting rid of segfaults (sigsegv) take roughly about 4-6 months
Hussein
but it worth it because C gives you almost the best performance you might get from a programming language
Hussein
also you will have a good understanding of your program internals and how to speed things up even more than how you would do with rust or c++
Hussein
Why ?
because when you pass a parameter to a function you are copying it’s content somewhere on the memory (usually the stack but sometimes cpu registers if the value is small enough) so if you have a big array then you pass it as a parameter to a function. you will end up with two instances on memory of the same array, this is why more memory will be required not mentioning that copying the content of a huge array might take sometime and will slow your program