warning: assignment makes pointer from integer without a cast

This is a discussion on warning: assignment makes pointer from integer without a cast within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <errno.h> int main(int argc,char **argv) { char ch[1000]; int max = 1000; ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    warning: assignment makes pointer from integer without a cast

    Code:
     
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <errno.h>
     
    int main(int argc,char **argv)
    {
      char ch[1000];
      int max = 1000;
      int i = 0;
      DIR        *dip;
      struct dirent    *dit;
      FILE *fp;
    
      char* result = NULL;
      char delims[] = {'(', '.' , ')'};
      /* read character by character from stdin */
      if((dip = opendir(argv[1])) == NULL){
        perror("opendir");
        return 0;
      }
        
      while((dit = readdir(dip)) != NULL){
        fp = fopen(dit->d_name, "r");
        printf("%s\n",dit->d_name);
        while (fgets(ch, sizeof(ch),fp ) != NULL){ 
          result = strtok( ch, delims );
          while( result != NULL ) {
            printf( "result is \"%s\"\n", result );
            result = strtok( NULL, delims );
          }
          //printf("%s ",ch);
          i++;
        }
      }
      printf("\n");
      return 0;
    }
    Okay i know this is common but I get that warning every time i use the strtok command I is there a reason i dont even using integers so..... im not sure if its "result" thats the problem

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Smile

    Use the following program.It will not get warning.
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <errno.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc,char **argv)
    {
      char ch[1000];
      int max = 1000;
      int i = 0;
      DIR        *dip;
      struct dirent    *dit;
      FILE *fp;
    
      char* result = NULL;
      char delims[] = {'(', '.' , ')'};
    
      if(argc<2)
      {
              printf("Give any directory\n");
              exit(1);
             }
      /* read character by character from stdin */
      if((dip = opendir(argv[1])) == NULL){
        perror("opendir");
        return 0;
      }
    
      while((dit = readdir(dip)) != NULL){
        fp = fopen(dit->d_name, "r");
        printf("%s\n",dit->d_name);
        while (fgets(ch, sizeof(ch),fp ) != NULL){
          strcpy(result , strtok( ch, delims ));
          while( result != NULL ) {
            printf( "result is \"%s\"\n", result );
            strcpy(result , strtok( NULL, delims ));
          }
          //printf("%s ",ch);
          i++;
        }
      }
      printf("\n");
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Hope this changes will help
    Code:
     if ( fp != NULL) // Check before reading a directory 
        while (fgets(ch, sizeof(ch),fp ) != NULL){
          result =(char *) strtok( ch, delims );  // type cast to avoid warnings 
          while( result != NULL ) {
            printf( "result is \"%s\"\n", result );
            result =(char *) strtok( NULL, delims ); // type cast to avoid warnings 
          }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > fp = fopen(dit->d_name, "r");
    What about calling fclose() at some point.

    > result =(char *) strtok( NULL, delims ); // type cast to avoid warnings
    And what warning would that be?
    I see no reason for a cast here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    char delims[] = {'(', '.' , ')'};
    Anyone else see a problem passing that into the strtok function as the second argument?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: cast to pointer from integer of different size
    By DavidDobson in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 05:37 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 10:08 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 3
    Last Post: 01-14-2002, 11:13 AM

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