basic doubt in pointer concept

This is a discussion on basic doubt in pointer concept within the C Programming forums, part of the General Programming Boards category; dear sir i am having a simple doubt in pointer concept. i am reading in 3 names using a string ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    basic doubt in pointer concept

    dear sir
    i am having a simple doubt in pointer concept.

    i am reading in 3 names using a string pointer,

    and printing them ,,

    it works out perfectly fine..

    but i get a "warning message"


    Integral value implicitly converted to pointer in assignment,
    i am using hp-unix system.

    i would be greatful if anyone could let me know why this message
    is thrown up by the compiler




    Code:
    #include <stdio.h>
    void main()
    {
       char *str[3];
       int i;
            printf(" enter the names of 3  students");
    
       for(i=0;i<3;i++)
       {
            str[i] = malloc(10);
            scanf("%s",str[i]) ;
       }
    
       printf(" the name of the first students is %s\n",*(str+0));
       printf("the name of th e second student is %s\n",*(str+1));
       printf("the name of the third student is %s\n",*(str+2));
    
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    void main is wrong, main must return an int and nothing else or the program is undefined. The correct definition of main is

    int main ( void )

    You must also return a value at the end of main, 0 is a portable value for successful termination.

    >str[i] = malloc(10);
    You use malloc yet I don't see stdlib.h included anywhere, this is undefined behavior and is the cause of your warning. Since malloc is not defined, the compiler assumes it is a function that returns int, yet you assign the return value to a pointer. Include stdlib.h and the problem will be fixed.

    >scanf("%s",str[i]) ;
    scanf is really unsafe when used like this because there is no way to be sure that you aren't reading more than 10 characters. Buffer overflow is a very real problem. I recommend fgets for reading string data.

    >printf(" the name of the first students is %s\n",*(str+0));
    This is okay for practice programs, but pointer notation should be avoided when possible because array notation is simpler and easier to understand at a glance:

    printf ( " the name of the first students is %s\n", str[0] );
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
     char *str[3] = {0}; /* NULL the three pointers */
     int i;
    
      for ( i = 0; i < 3; i++ ) {
        printf ( "Enter the name of a student: " );
    
        if ( ( str[i] = malloc ( 10 ) ) == NULL ) {
          printf ( "Error allocating memory\n" );
    
          /*
          ** Don't do this, free the memory you've allocated
          ** before terminating the program.
          */
          exit ( EXIT_FAILURE );
        }
    
        fgets ( str[i], 10, stdin );
      }
    
      printf ( "The name of the first student is %s\n", str[0] );
      printf ( "The name of the second student is %s\n", str[1] );
      printf ( "The name of the third student is %s\n", str[2] );
    
      free ( str[0] );
      free ( str[1] );
      free ( str[2] );
    
      printf ( "Hit return to exit" );
      getchar();
    
      return EXIT_SUCCESS; /* EXIT_SUCCESS is defined in stdlib.h */
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM
  2. basic doubt
    By vikranth in forum C Programming
    Replies: 7
    Last Post: 10-30-2007, 12:21 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21