EDIT -> My problem with finding if was an integer got solved. My next problem is how to get the size of the array. Example:
char **str = [hello][world] -> returns 2
char **str = [hello] -> return 1
char **str = [hello][world][yes] -> returns 3
The way I am doing right now. It's wrong
int size = sizeof(str) / sizeof(str[0]);
I also tried
int size = sizeof(str) / sizeof(char *);
.....
I have a program that gets a line from the user like "Hello World".
Then I parse that into an array of pointers: so it's something like char **str = [Hello][World]
.
Then I want to check if the second index is an integer or not. So if the user types Hello 2
, this function returns true (1), but if not returns false(0).
I am a little confused how to work with pointers.
So I think I will have something like this:
int my_function(char** str) {
// it's empty
if (str[0] == NULL)
return 0;
// Just have one index
int size = sizeof(str) / sizeof(str[0]);
if (size < 1) {
return 0;
}
// checking if data in second index is an integer
int length = strlen(str[1]);
for (int i = 0; i < length; i ) {
// Here is my problem how do I go over each character for
// only str[1] to check if is an integer???????
if (!isdigit(str)) {
return 0;
}
}
return 1;
}
CodePudding user response:
You need a double dereference. The first dereference to get the relevant char pointer. The second dereference to get the relevant character.
Try:
isdigit(str) --> isdigit(str[1][i])
CodePudding user response:
Try following,
char *secondString = str[1];
int length = strlen(secondString );
for (int i = 0; i < length ; i)
{
if (!isdigit(secondString[i]))
return 0;
}
return 1;