Thread: expected ‘const char *’

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    7

    expected ‘const char *’

    Hello everyone I am still new to c but going throught a book called Learn c on the mac but I am using Ubuntu. I wrote a easy little program in witch it will ping all ip addresses from 192.168.6.1 to 192.168.6.254 once and print the responce out. I am getting "/usr/include/stdio.h:870:14: note: expected ‘const char *’ but argument is of type ‘int’" but I can't figure out why becuase I get the error on this line.
    Code:
                fp = popen(ping, 'r');
    but they are declared like this
    Code:
     
            char ping[30];
            FILE *fp;
    here is the whole thing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
        main(){
            int count = 0;
            char ping[30];
            FILE *fp;
            char op[200];
            while( count = 1, count <= 254, count++) 
            {
                sprintf(ping, "ping -c 1 192.168.6.%d", count);
                fp = popen(ping, 'r');
    
                  while (fgets(op, sizeof(op)-1, fp) != NULL) 
                      {
                            printf("%s", op);
                      }
            } 
    
    
    
    
    
            return 0;
            }
    I have been seraching the internet but I am still unable to figure out why I am getting this, anyone have an idea?
    Thank you to anyone who replies.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    FILE *popen(const char *command, const char *type);

    The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing.
    (source: popen(3): pipe stream to/from process - Linux man page)

    Note you're using a single character ('r') as the second argument. Make it a string by using double quotes instead ("r").

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    7
    Thanks for your help, that did the trick I'm not sure I understand why but I look in to the man pages.
    Thanks Again.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by willane1965 View Post
    Thanks for your help, that did the trick I'm not sure I understand why but I look in to the man pages.
    'r' represents a single character.

    "r" represents a c-string (which is a series of zero or more characters terminated with a null character, '\0').

    More reading: C Strings - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected expression before const
    By ingeniousreader in forum C Programming
    Replies: 11
    Last Post: 03-06-2012, 04:34 PM
  2. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  3. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  4. expected primary expressio before const
    By mrlemke in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2009, 11:50 AM
  5. Replies: 7
    Last Post: 04-28-2008, 09:20 AM