I'm trying to work with C for the first time. I have a program that calls a bash script. This script printf
's a number via
COUNT=$(ls | grep tool | wc -l)
printf "%s" "$COUNT"
I want to grab this number and perform a simple math operation on it. However, I can't seem to be able to convert this value into an int
so that I can divide it by 2.
I've tried things like https://stackoverflow.com/a/868508/183254
int myint = otherint - '0';
which produces a segmentation fault when I got it to compile and it attempted to do the math.
I've also tried many iterations of
int cc;
cc = count - '0';
// warning: assignment makes integer from pointer without a cast [enabled by default]
// or
// warning: initialization from incompatible pointer type [enabled by default]
to no avail.
I've read several posts that came up for the google search for those errors, but still can't seem to be able to wrap my head around what is going on I think perhaps because I don't have the context of what the questioner is trying to achieve.
Here is a little demo I put together to try and understand what is going on. Turns out it just made me more confused.
int main(){
char *x;
x="44";
int xx;
xx = (int) x - '0';
printf("x is : %s\n", x);
printf("xx is : %d\n",xx);// x is : 23703288
char command[64];
char path[9];// won't work if set to <=8
int status;
FILE *fp;
//this shell script just counts the number of files in a directory
//that match a certain string.
strcpy(command, "/test/test.sh");// returns 0-9; in this case it's returning 4 (supposed to anyway)
fp = popen(command, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 9, fp) != NULL) //again, won't work if arg2 is <=8
//printf("%s", path);
status = pclose(fp);
int cc;
cc = (int) path - '0';
printf("result of path is %s\n",path ); // result of path is 4
printf("result of conversion is %d\n", cc);// result of conversion is 1582717056
}
output:
x is : 44
xx is : 182607602
result of path is 4
result of conversion is 1423813191
I've found and read a couple of primers but I don't know if they are old or what but the didn't help much besides explaining pointers a bit.
What really boggles my mind is what is going on in the snippet above:
x is : 44
Ok, that makes sense since I typed out x="44";
.
xx is : 182607602
wat. This looks like it must be a pointer to some address. According to the primers I read, this should come from something like &x;
; why is it coming from what should be converting the char 44
to int 44
?
result of path is 4
Ok (except all of those spaces). It almost seems like it's being padded or something. char path[9];
and while (fgets(path, 9, fp) != NULL)
heighten my suspicion, but as commented won't work <= 8.
result of conversion is 1423813191
Same; seems to be pointing to address space. Don't understand why.
It seems to be a point of confusion for many people (including me) coming from other languages -- that's my excuse and I'm sticking to it.
Any pointers would be greatly appreciated (good primers, what's going on in this code, anything really).
Thanks.
Found an excellent 9-part series on youtube which does a great job explaining some C fundamentals: https://www.youtube.com/playlist?list=PLkB3phqR3X40reMCBYSoNUPbDvM4kybMs. Part 7 was especially useful -- it covers pointers which was a big part of my confusion.