SR27
The post decrement also requires an lvalue. The difference with predecrement is that predecrement returns an lvalue while post decrement returns an rvalue.
#include<stdio.h> #include<stdlib.h> int main() { char *ptr[2] ; for(int i=0; i<2; i++) { ptr[i] = (char *)malloc( 1 ); printf("Enter the %d string \n", i+1); scanf("%s", ptr[i] ); } for(int i=0; i<2; i++) printf("\n%s", ptr[i] )
SR27
input is 1 string good 2string morning
SR27
is this correct?
SR27
what i think is we are assigning only 1 byte in Heap and input is more than 1 byte so is this wrong ?
SR27
You are right.
even it prints good morning but it still is logical error ?
Anonymous
even it prints good morning but it still is logical error ?
Its a out of range error, a type of logic error.
SR27
Its a out of range error, a type of logic error.
as a good programmer we should not use or play with unallocated memory
SR27
?
Anonymous
as a good programmer we should not use or play with unallocated memory
Right. You should always allocate sufficient memory.
Anonymous
#include<stdio.h> #include<stdlib.h> int main() { char *ptr[2] ; for(int i=0; i<2; i++) { ptr[i] = (char *)malloc( 1 ); printf("Enter the %d string \n", i+1); scanf("%s", ptr[i] ); } for(int i=0; i<2; i++) printf("\n%s", ptr[i] )
It so happens that in this case the heap allocations for both the pointers happened in relatively far away locations. But assume that they had been allocated next to each other. In that case when you input the second string, you will overwrite the previous string you had input and the output would be jumbled up. It is precisely for this reason that it is Undefined Behavior.
Anonymous
so basically this we should not use or play with unallocated memory ?
And in future when you ask a question, please dont reply to a message of an unrelated thread. Just ask the question directly
Bharti
#include<iostream> using namespace std; class node{ public: int data; node* next; node* prev; node(int val){ data=val; next=NULL; prev=NULL; } }; void insert(node* &head,int val){ node* n=new node(val); node* temp=head; if(head==NULL){ head=n; return; } else{ while(temp->next!=NULL){ temp=temp->next; } temp->next=n; n->prev=temp; } } void display(node* head){ node* temp=head; if(head==NULL){ cout<<"list is empty"<<endl; } else{ while(temp!=NULL){ cout<<temp->data<<"->"; temp=temp->next; } cout<<"NULL"<<endl; } } void alternatedel(node* &head){ node* temp=head; while(temp->next->next!=NULL && temp->next!=NULL){ temp->next=temp->next->next; temp->next->next->prev=temp; temp=temp->next->next; } temp->next=NULL; } int main(){ node* head=NULL; insert(head,1); insert(head,2); insert(head,3); insert(head,4); insert(head,5); insert(head,6); display(head); alternatedel(head); display(head); return 0; }
Bharti
i want to delete the alternate nodes
Bharti
But this is showing error
Sonu
Read my message again
Ok I got it..! 😅
Anonymous
But that didn't created any problems before
It wont. It is just that it is discouraged and is not considered a good programming practice in C++.
Anonymous
In which func?
The same func alternatedel
Bharti
I think there is some other mistake too
Anonymous
I think there is some other mistake too
Did you try both the changes I suggested? Show me your latest code and put it on pastebin.com. Dont paste it here.
Anonymous
Hi
Anonymous
I need help
Anonymous
If anyone can help
Anonymous
int [] intArray = new int[] {1, 1, 1,-1, 1}; println("Original Array:"); for (int i=0; i<intArray.length; i++) print(intArray[i] + " "); println(); println(" reverse order:"); for (int i=intArray.length-1; i>=0; i--) print(intArray[i] + " "); ------------------------------------------------------ i want to reach this result {1, 1, 1,-1, 1} orginal * reverse {1,-1, 1, 1, 1} 1 1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 1 ------------------------------------------------------ 1 0 1 0 5 0 1 0 1
Bharti
Can I share the fun only ?
Bharti
void alternatedel(node* &head){ node* temp=head; while(temp!=NULL && temp->next!=NULL && temp->next->next!=NULL){ temp->next=temp->next->next; temp->next->next->prev=temp; temp=temp->next->next; } temp=temp->next; }
Bharti
And the last line in while loop should be temp = temp->next;
But I think after the while loop ends you are in the last node that means the next of the last node should be null .
Mame ¥£~~©©\[§
Hello everyone How are you doing ??
Anonymous
But I think after the while loop ends you are in the last node that means the next of the last node should be null .
Sorry was away. Try this instead: https://pastebin.com/TLzsthrt (Simplified it further). I havent tested it. So see if it works and let me know.
Bharti
Sorry was away. Try this instead: https://pastebin.com/TLzsthrt (Simplified it further). I havent tested it. So see if it works and let me know.
void alternatedel(node* &head){ node* temp=head; node* n=head->next; while(temp!=NULL && n->next!=NULL){ temp->next=temp->next->next; n->next->prev=temp; temp=n->next; n=n->next->next; } temp->next=NULL; }
Bharti
that was completely wrong
Bharti
thank you btw😁😁
Anonymous
that was completely wrong
You mean my function was wrong. Let me test it.
Bharti
You mean my function was wrong. Let me test it.
No sorry I haven't tested it
Bharti
You mean my function was wrong. Let me test it.
I mean my earlier code was completely wrong
Anonymous
I mean my earlier code was completely wrong
This is the code btw : https://pastebin.com/TLzsthrt
Anonymous
Okay .thank you
There is a semicolon missing in Line 6. Found that while trying to compile it. Just add that semicolon there.
Bharti
sure!!
Anshul
Can anyone suggest me about what are the projects needed to be placed in a product based company I have no knowledge about what actually a project is.
Anonymous
Can anyone suggest me about what are the projects needed to be placed in a product based company I have no knowledge about what actually a project is.
i don't think asking questions related to these are allowed here if it is, there would be a separate group maybe
Mac
If I have: int *mypointer; cout << &mypointer << endl; cout << mypointer << endl; What is the difference between these two?? The first gives you the address of the pointer, yes? What is the second?
@𝑺𝒐𝒃𝒌𝒂
If I have: int *mypointer; cout << &mypointer << endl; cout << mypointer << endl; What is the difference between these two?? The first gives you the address of the pointer, yes? What is the second?
The first one print the address of mypointer. The second print the adress of the object mypointer point to. You should initialize the pointer until you use it (particularly on local scope).
@𝑺𝒐𝒃𝒌𝒂
That makes sense so &mypointer prints the pointer to the pointer.
Nop. int *mypointer is a pointer to an object of type int. You can write pointer to a pointer as: type **mypointer;
ᴿᴵᴷ
I need help, I have a 1 hour exam in c++
Anonymous
template<typename T> concept Equal = requires(T a, T b) { { a == b } -> std::convertible_to<bool>; { a != b } -> std::convertible_to<bool>; };
Anonymous
template<typename T> concept Equal = requires(T a, T b) { { a == b } -> std::convertible_to<bool>; { a != b } -> std::convertible_to<bool>; };
I know that this is a definition of a concept but could someone explain to me the syntax of these two lines: { a == b } -> std::convertible_to<bool>; { a != b } -> std::convertible_to<bool>;
Anonymous
I know that this is a definition of a concept but could someone explain to me the syntax of these two lines: { a == b } -> std::convertible_to<bool>; { a != b } -> std::convertible_to<bool>;
It just declares a concept Equal which will be satisfied by any type T if the values a and b of type T can be compared 1. using == and the value of this expression is convertible to bool 2. using != and the value of this expression is convertible to bool
Yorhane
hello guys, i'm not able to open a .bat file using c language does anyone have any idea what it could be?
Yorhane
Does system("<name.bat>") not help?
broo, The syntax of the command is incorrect.
Anonymous
broo, The syntax of the command is incorrect.
???? The angle brackets are for illustration. If your batch file name is a.bat, then try system("a.bat"). If that doesnt work try system("%ComSpec% /C a.bat")
Anonymous
It was a console based app. I compiled it on a 64bit and executed on a 32 bit computer.
How do you expect 64 bit machine instructions to run on a 32 bit system?
Anonymous
This is the code btw : https://pastebin.com/TLzsthrt
You forgot to delete the discarded nodes. https://pastebin.com/qAzdTRSa
Anonymous
You forgot to delete the discarded nodes. https://pastebin.com/qAzdTRSa
The OP didnt do deletion in her program. So I didnt either. Just modified the OP's pointer code.
Yasas
But I don't use gcc much due to using an ide. I use clion and it compiles the code for me. So how can I use the flag?
Anonymous
Yasas
Ok
Yasas
Thank you sir or mam who ever you are.
Suka
Why?
too heavy for my pc hehehe. so i am avoided java based ide. except eclipse, i use it to generate project makefile(embedded/baremetal) and use qtcreator or vim-coc to coding hehehe.