4 Replies - 333 Views - Last Post: 21 March 2010 - 08:53 PM Rate Topic: -----

#1 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

C is giving some trouble

Posted 21 March 2010 - 07:52 PM

Good night guys. Im not sure what is wrong with this my code. Im working with a link list (i think this is a link list). Here is the code(below im going to tell you what is happening):
/*Write a program that creates a link list of 10 characters,
then creates a copy of the list in reverse order*/

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

// self-referential structure
struct linkList_data
{
    char data;   //each listNode contains a character
    struct linkList_data *nextPtr;	//pointer to next node
};	//end structure linkList_data

	char linkList_dataPtr, *sPtr;
	char value;
    char newPtr;      // pointer to new node
    char previousPtr; // pointer to previous node in list
    char currentPtr;  // pointer to current node in list
    linkList_data;
int main ()
{
    newPtr = malloc( sizeof( linkList_data ) ); //create node

    if ( newPtr != NULL )   //is space available
    {
        newPtr -> data = value; // place value in node
        newPtr -> nextPtr = NULL; //node does not link to another node

        previousPtr = NULL;
        currentPtr = *sPtr;

        // loop to find the correct location in the list
        while ( currentPtr != NULL && value > currentPtr->data )
        {
            previousPtr = currentPtr;          /* walk to ...   */
            currentPtr = currentPtr->nextPtr;  /* ... next node */
        } // end while

        // insert new node at beginning of list
        if ( previousPtr == NULL )
        {
            newPtr -> nextPtr = *sPtr;
            *sPtr = newPtr;
        } //end if
        else   //insert new node between previousPtr and currentPtr
        {
            previousPtr -> nextPtr = newPtr;
            newPtr -> nextPtr = currentPtr;
        } //end else

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


 getchar ();
 return 0;
} //end main


When i run the program i get 3 errors "cannot convert 'void *' to 'char'", "Declaration terminated incrrectly" and "Pointer to structure required on left side of -> or ->*" the last 1 i got about 6 times. what can be wrong with it?
Please help!!!!
Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: C is giving some trouble

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: C is giving some trouble

Posted 21 March 2010 - 08:02 PM

>>When i run the program i get 3 errors "cannot convert 'void *' to 'char'"

That indicates that you are compiling it as a c++ program, not a C program. C does not require typecasting void* to something else, but C++ does require explicit type casting. Change the extension of the file name from cpp (or cc if *nix) to *.c and compile again. With *.c the compile will compile it as a C program.

After you correct the file name then some or all of the other errors may be resolved. Post back if not.
Was This Post Helpful? 0
  • +
  • -

#3 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Re: C is giving some trouble

Posted 21 March 2010 - 08:25 PM

View PostAncient Dragon, on 21 March 2010 - 07:02 PM, said:

>>When i run the program i get 3 errors "cannot convert 'void *' to 'char'"

That indicates that you are compiling it as a c++ program, not a C program. C does not require typecasting void* to something else, but C++ does require explicit type casting. Change the extension of the file name from cpp (or cc if *nix) to *.c and compile again. With *.c the compile will compile it as a C program.

After you correct the file name then some or all of the other errors may be resolved. Post back if not.


Thanks
But its not working. im getting the same errors :(
Was This Post Helpful? 0
  • +
  • -

#4 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Re: C is giving some trouble

Posted 21 March 2010 - 08:38 PM

/*Write a program that creates a link list of 10 characters,
then creates a copy of the list in reverse order*/

#include<stdio.h>
#include<conio.h>

void read(char *, char);
void dis(char *,char);
void inverse(char *,char *,char);

struct lists
{
     int intList[10];
     char charList[10];
};


main(){

   char a[10],b[10];
   read(a,10);
   dis(a,10);
   inverse(a,b,10);
   dis(b,10);
   getch();
}

void read(char c[],char i)
{
   char j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%s",&c[j]);
   fflush(stdin);
}
void dis(char d[],char i)
{
   char j;
   printf("The list is \n");
   for(j=0;j<i;j++)
      printf("%s ",d[j]);
   printf("\n");
}
void inverse(char a[],char inverse_b[],char j)
{
   char i,k;
   k=j-1;
   for(i=0;i<j;i++)
   {
      inverse_b[i]=a[k];
      k--;
   }
}



I have that code that im working with but when it run and its time to print out the list in revers is it crashing
Was This Post Helpful? 0
  • +
  • -

#5 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: C is giving some trouble

Posted 21 March 2010 - 08:53 PM

void read(char c[],char i)
{
   char j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%s",&c[j]);
   fflush(stdin);
}



parameter c is an array of single characters. For example it could contain the string "Hello", in which case it is an array of 6 characters plus null terminator. In the loop, and parameter to scanf(), you are attempting to treat it as if it were a two dimensional array of strings, such as char c[][20];. All you are passing to scanf() is a pointer to a single character.

The second problem with that function is the use of scanf(). scanf() is a very dangerous function because it will allow you to type more characters into the input buffer than it can hold. In your program, what do you think will happen if I type "Hello"? Answer: scanf() will probably just scribble the characters all over memory because there is only room for only one character. And that is exactly why your program crashed.

How to fix:
int main()
{
   char c[20][40]; // room for 20 strings, 40 characters per string

   read(c, 20, 40);
}

void read(char c[][40], int asize, int length)
{
   int i;
   for(i = 0; i < asize; i++)
   {
     scanf("%*s", length, &c[i]);
   }
}



In the above read function, The "%*.s" is telling scanf() to limit the string to the length specified in the first parameter -- that's what the * is for.

This post has been edited by Ancient Dragon: 21 March 2010 - 08:55 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1