USING MALLOC

hi.. i am posting this thread because i hoping that somebody can help me about my problem. this freakn me out. i checked the code well and i got this 1 irritating error that i can't fixed out.

my code is this

newPtr = malloc(sizeof(ListNode));

where ListNode is my alias of my strcut definition i had declare earlier.
what is the problem that my compiler says "cannot convert void * to listnode *".. what is the problem with my code?

#thanks i hope you can help
The problem is that this code is not valid C++. It is valid C, though, perhaps you meant to use a C compiler?

Equivalent C++ would be newPtr = new ListNode;

(or you could force your original line to compile with newPtr = static_cast<ListNode*>(malloc(sizeof(ListNode))); or equivalent explicit cast, but, depending on the contents of ListNode, it may or may not fail at runtime)
Simple.
I'm telling you two things:
1. You're using C and not C++.
In C, you use malloc and free.
In C++, you use new and delete.

2. There are two solutions.
A. The C solution:
1
2
3
newPtr = (ListNode*)malloc(sizeof(ListNode)); // Allocate it
// Use it
free(newPtr); // Free it 

B. The C++ solution:
1
2
3
newPtr = new ListNode; // Allocate it
// Use it
delete newPtr; // Free it 
A. The C solution:


A C solution should not include a cast.

http://stackoverflow.com/questions/3559656/casting-void-pointers
@cire:
I didn't know about that.
But yet his problem is related to using C code for C++.
Also, I'm sure you can cast pointers in C, and that isn't of any trouble.


im using a c++ compiler.. with an extension of .cpp. then im also using printf and scanf


is it not possible to compile turbo c code in a c++ compiler? what compiler do i need to download?
You should use the C++ solution.
You should not use C anymore.
is it not possible to compile turbo c code in a c++ compiler?

You could give the file an extension of .c rather than .cpp.

But bear in mind that the Turbo C compiler, though good in its time, allowed (or forced) the use of some non-standard code. It also had the occasional bug, which might have forced the use of idiosyncratic code to work around the defect.

If you have old code of your own, then I'd recommend you update it to modern standards.
I change the extension to .c

i don't have an error anymore. but when i run it, i inserted a letter, it loops forever. this are my codes . . . please help

#include<stdio.h>
#include<stdlib.h>

struct listNode
{
char data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert(ListNodePtr *sPtr, char value);
char delet(ListNodePtr *sPtr, char value);
int isEmpty(ListNodePtr currentPtr);
void printList(ListNodePtr currentPtr);
void instructions(void);

int main()
{

ListNodePtr startPtr =NULL;
int choice;
char item;
instructions();
printf("?");
scanf("%d", &choice);
while(choice !=3)
{
switch(choice)
{
case1:

printf("Enter a character:");
scanf("\n%c", &item);
insert(&startPtr, item);
printList(startPtr);
break;

case2:
if (!isEmpty(startPtr))
{
printf("Enter character to be deleted:");
scanf("\n%c", &item);
if(delet(&startPtr, item ))
{
printf("%c deleted.\n",item);
printList(startPtr);
}
else
printf("%c not found.\n\n item");
}
else
printf("List is empty.\n\n");
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf("?");
scanf("&d", &choice);
}
printf("End of run.\n");
return 0;
}
void instructions(void)
{
printf("Enter your choice.\n");
printf("1 to insert an element into the list.n");
printf("2 to insert an element into the list.\n");
printf("3 to end.\n");
}
void insert( ListNodePtr *sPtr, char value)
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;

newPtr=malloc(sizeof(ListNode));

if(newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr;

while(currentPtr!=NULL&&value>currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(previousPtr==NULL)
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}

}
else
printf("%c not inserted. No memory available.\n", value);
}

char delet(ListNodePtr *sPtr, char value)
{
ListNodePtr previousPtr, currentPtr, tempPtr;

if(value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sPtr;
currentPtr=(*sPtr)->nextPtr;

while(currentPtr!=NULL && currentPtr->data!=value)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if(currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr;
free(tempPtr);
return value;
}
}
return '\0';
}

int isEmpty(ListNodePtr sPtr)
{
return sPtr==NULL;
}

void printList(ListNodePtr currentPtr)
{
if(currentPtr==NULL)
printf("List is empty.\n\n");
else
{
printf("The list is: \n");

while(currentPtr!=NULL)
{
printf("%c --> ", currentPtr->data);
currentPtr=currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}
Topic archived. No new replies allowed.