Reputation: 1965
Can someone tell me how to correct this warning/error. I am trying to get just the first character of a string to tell if it a "-".
Error:
grep-lite.c:15:13: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
if(strcmp((char *) pattern[0],"-") == 0)
^
grep-lite.c:29:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
while(strcmp((char *) argv[index][0],"-"))
^
Source with warnings/errors:
Line 15:
if (strcmp((char *) pattern[0],"-") == 0)
Line 29:
while (strcmp((char *) argv[index][0],"-"))
Complete Source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grep-lite.h"
int main(int argc, char * * argv)
{
//initailize variables
int index = 1, lineNumber = 1;
int oneMatchingLine = FALSE;
int invertOpt = FALSE, lineNumOpt = FALSE, quietOpt = FALSE;
char * pattern = argv[argc];
//check if last arguement is invalid by starting with a '-'
if(strcmp((char *) pattern[0],"-") == 0)
{
error(INVALID_PATTERN);
return EXIT_ERROR;
}
//check if they asked for help
if(strcmp(argv[index],"--help") == 0)
{
printHelpStatement();
return EXIT_SUCCESS;
}
//walk through all options
while(strcmp((char *) argv[index][0],"-"))
{
//find and set option
if(processOptions(argv[index], &invertOpt, &lineNumOpt, &quietOpt))
index++;
//if invalid option fai
else
{
error(INVALID_OPTION);
return EXIT_ERROR;
}
}
//walk through stdinput searching for pattern relationship
while(feof(stdin) == 0)
{
//initialize
char * thisLine = NULL;
// get read line with fgets
thisLine = fgets(thisLine, MAX_CHARACTERS, stdin);
//find pattern location in thisLine
char * patternLoc = strstr(thisLine, pattern);
//check if we should print this line based of patternLoc and invertOpt
if((!patternLoc != NULL && !invertOpt) || (pattenLoc == NULL && invertOpt))
{
//see if we should print this line
if(!quietOpt)
printLine(thisLine, lineNumOpt, lineNumber);
}
lineNumber++;
}
Upvotes: 0
Views: 718
Reputation: 53006
I will enumerate the problems I find in your code
The correct usage of strcmp()
exists in your code, in this line
if (strcmp(argv[index],"--help") == 0)
strcmp()
is intended for string comparison, not character comparison, this
if(strcmp((char *) pattern[0],"-") == 0)
should be
if (pattern[0] == '-')
do not cast the variable to force compilation, instead find the actual cause for the compiler error/warning.
You have a severe error, you don't allocate space for the thisLine
char
pointer, you must allocate memory via malloc()
or just declare it as a char
array like
char thisLine[SOME_CONSTANT_NUMBER];
Also, this
while(feof(stdin) == 0)
is never a good idea instead do it like this
char thisLine[100];
while (fgets(thisLine, sizeof(thisLine), stdin) != NULL)
You made another very common mistake, arrays in c are indexed from 0
to N - 1
, so
char *pattern = argv[argc]
is wrong, because you are reading one element after the last, the correct code is
char *pattern = argv[argc - 1]
which will give you the last element.
Upvotes: 1