Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i do not understand how to delete addres pointer.

but in my script eror when delete (coba);

example :
Input :
2 3
2 5
2 6
1 2
3
1 2
Output :
Value =5;
(5 deletting)
Value =6;

soo (*coba).nilai = 3 , 6..
5 is delete..
but i do not understand how to do it..
helpp

What I have tried:

#include <iostream>
#include <vector>
 
using namespace std;
 
struct test
{
  int next;
  int previous;
  int value = 0;
};

int main()
{
	test *coba, testt;
	coba = &testt;
	int z;
	int l = 0;
	int perintah;
	int y = 0;
	while(true)
	{
		cout << "Input : ";
		cin >> l;
		if(l == 2)
		{
			cin >> (*coba).value;
			*(++coba);
			y++;
		}
		if(l == 1)
		{
			cin >> z;
			for(int i = 0; i < y-z+1; i++)
			{
				(--coba);
			}
			cout << "Value = " << (*coba).value << endl;
			cout << "1.next / 2.previous / 3.erase : ";
			cin >> perintah;
			if(perintah == 1)
			{
				(++coba);
				testt.next = (*coba).value;
				cout << "Next = " << testt.next << endl;
				for(int i = 0; i < y-z; i++)
				{
					(++coba);
				}
			}
			else if(perintah == 2)
			{
				(--coba);
				testt.previous = (*coba).value;
				cout << testt.previous;
				for(int i = 0; i < y-z+2; i++)
				{
					(++coba);
				}
			}
			else if(perintah == 3)
			{
				delete (&coba);
			}
		}
	}
  return 0;
}
Posted
Updated 11-Jul-18 19:54pm
Comments
KarstenK 12-Jul-18 2:49am    
Normally C++ written this style: coba->value.

Use delete ONLY for pointers which values are to memory (or objects/structs) which are allocted with new. (It is a fundamental rule!!!)
CPallini 12-Jul-18 3:42am    
What are you trying to do? Are you implmenting a linked list? Why don't you consider using std::list instead?

1 solution

As I told you yesterday: Error C++ struct with pointer[^] THOSE AREN'T POINTERS.
C++
struct test
{
  int next;
  int previous;
  int value = 0;
};
next and previous cannot be declared as int - they need top be pointer values and preferably pointers to the actual destination type:
C++
struct test
{
  struct test *next;
  struct test *previous;
  int value = 0;
};
If you don't, then you will get problems. Integers are 32 bit values (in a modern compiler), but on a 64 bit system pointers are 64 bit values: so when you set a pointer to an address you need a destination that has enough space to store an entire 64 bit value, if you don;t have that, then something is going to get overwritten...

To add to that, your "next" code is total garbage!
C++
test *coba, testt;
	coba = &testt;
...
	(++coba);
	testt.next = (*coba).value;
No, you cant just do that - you are using memory you haven't allocated whine you use ++, so you will again start corrupting memory. NAd that last line? What the heck do you expect that to do?

Stop guessing. Read your course notes, and start thinking. Hope and pray is not a viable design strategy...
 
Share this answer
 
Comments
Member 13906640 12-Jul-18 2:08am    
I just learned the pointer yesterday, and I can only learn to see from the internet, this is very difficult for me
Member 13906640 12-Jul-18 2:17am    
I was told to make it only with struct and pointer, I have tried my best but it is very difficult :(
Member 13906640 12-Jul-18 2:22am    
I think to add its next value by:
*(++coba);
it's wrong?
OriginalGriff 12-Jul-18 3:19am    
Yes.
Because coba points at memory which only has room for one instance of the struct.

Seriously: if you are trying to learn this stuff by "see from the internet" then you are never going to get anywhere: Go on a course; get a book (your local library may be able to find you some) - you need to have this stuff explained to you in a structured way, not random presented by code you clearly don't understand yet.

And we can't teach you: this is a little tiny text box, not a book or a classroom! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900