Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 30th, 2005, 9:42 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Problem passing multi-dimensional array to function

Hello, I receive the following error when compiling my program.

p8.c: In function `main':
p8.c:20: warning: passing arg 2 of `CountCharacter' from incompatible pointer type

I can see the problem is that I'm not passing the type of data I think I am. I basically want to pass a command line argument which is a filename to my function.

My understanding is that when I call CountCharacter(TargetChar, argv[i]), argv[i] is the address of where the filename string begins. Please help me out.

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

int CountCharacter(char, char []);

int main(int argc, char *argv[])
        {
        char TargetChar;
        int i;

        if(argc < 2 || strlen(argv[1]) != 1)
                {
                printf("Usage: %s character [file1]...\n", argv[0]);
                exit(1);
                }

        TargetChar = argv[1][0];

        if(argc == 2)
                printf("File: %s  Character: %c  Number: %d\n", "stdin", TargetChar, CountCharacter(TargetChar, stdin));
        else
                {
                for(i = 2; i < argc; i++)
                        printf("File: %s  Character: %c  Number: %d\n", argv[i], TargetChar, CountCharacter(TargetChar, argv[i]));
                }

        return 0;
        }

int CountCharacter(char TargetChar, char file[])
        {
        FILE *fp;
        int Num = 0, Ch;

        if(strcmp(file, "stdin"))
                if((fp = fopen(file, "r")) == NULL)
                        {
                        printf("Can't open file %s for read\n", file);
                        return -1;
                        }

        while((Ch = fgetc(fp)) != EOF)
                {
                if(Ch == TargetChar)
                        Num++;
                }

        if(fclose(fp) != 0)
                printf("Error closing file %s\n", file);

        return Num;
        }
aznluvsmc is offline   Reply With Quote
Old Aug 30th, 2005, 9:46 PM   #2
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Please ignore this. The problem was that I wasn't passing stdin as a string.
aznluvsmc is offline   Reply With Quote
Old Aug 30th, 2005, 10:00 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Glad to see you got it working.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC