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

main(int argc, char * argv)

{
   FILE *fp = fopen ("C:file.c", "r");
   int i, par, ch;
   
   
   for (i=par=0; (ch=fgetc(fp))!=EOF ; )
    {
     i++;
     if (ch == '\n')
      {
       par++;
       if(argc == 2)
        if ((argv[1][0] == '-') && (argv[1][1] == '1'))
        printf("\n%d: ", par);
        printf("%d\n",i);
       i=0;
     }
    }
}



The error is in

Code:
  if ((argv[1][0] == '-') && (argv[1][1] == '1'))


What am I doing wrong?
Your declaration of main is wrong. It should be :
Code:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)

That's because in your declaration, you say argv is an array of chars (ie bytes), but it's actually an array of strings (ie an array of arrays of chars).
argv is a char*, which means it's a pointer to a character (or it's a string. argv is supposed to be a string array, so you should have:

Code:
main(int argc, char ** argv)

or

Code:
main(int argc, char * argv[])
merthsoft wrote:
argv is a char*, which means it's a pointer to a character (or it's a string. argv is supposed to be a string array, so you should have:

Code:
main(int argc, char ** argv)

or

Code:
main(int argc, char * argv[])


matrefeytontias wrote:
Your declaration of main is wrong. It should be :
Code:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)

That's because in your declaration, you say argv is an array of chars (ie bytes), but it's actually an array of strings (ie an array of arrays of chars).


Thanks guys.

Also, a string and an array of chars are different things :p.
Not in C actually (excepting if you say that in C, strings don't exist).
Really? I thought an array of chars looked like this:

{'a','b','c'}

and an array of strings looks like this:

{"string","string2"}
No, in C, which you are using, there is no such thing a string, as a string variable is a class object in C++. In C, strings are simply character arrays.
Interesting, I tried using puts with a char type array and it works. I only have to put a '\0' at the end.
AliceIsDead wrote:
Interesting, I tried using puts with a char type array and it works. I only have to put a '\0' at the end.


Yep, that is the way that a "C string" works. It just tells the program where the end of the string is, generally referred to a "Null termination"
kay, thanks
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement