Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have searched for any reference to this for quite a bit now but I haven't had any success, so I thought I would ask here. Basically, I am trying to understand a C written program for creating a shell in linux and I am having problems with this piece of code
...
else if (args[0][0]-'!'==0){
int x = args[0][1]-'0';
int z = args[0][2]-'0';
...
}
The args is storing the command's entered by the user. For instance, later the address space of the child (parent process reads the commands, child executes them) is replaced using a call to execvp(args[0], args). The definition of args is as follows: char *args[MAX_LINE/2 +1];
What I have been having trouble understanding is the ways in which the array is accessed; specifically what is meant by these expressions in this context:
args[0][0]-'!'==0
args[0][1]-'0';
args[0][2]-'0';
Judging by the name of the variable, args stands for a list/array of arguments.
arg[0] is the first element of that array.
args[0][0] is the first character of the first element of that array.
The expression args[0][0]-'!'==0 checks whether that character is equal to '!'. That could have been written better as args[0][0] == '!'.
It's as if instead of using if ( i == 10 ), you decide to use if (i-10 == 0).
The next two lines
int x = args[0][1]-'0';
int z = args[0][2]-'0';
expect that the second and third characters of the first argument are digits and extract the decimal values they correspond to. If the first argument is "!26", then x will have the value 2 and z will have the value 6.
That logic depends on the guarantee that the encoding used for the characters '0' - '9' are required to be contiguous.
Probably args is a reference to
int main(int argc, char **argv);
Then args[0] is the name of the program and in the following args you would find the Arguments of the Programm, see e.g. Arguments to main in C
Thus args[0[0] is the first character of the name of the program.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am designing a program in C to detect tokens.
I got confused in the 4th line. What does the (right-left+2) do?
char* sub_string(char* str, int left, int right)
{
int i;
char* sub_str = (char*) malloc(sizeof(char)*(right-left+2)); /* 4th line */
}
right-left+2 in this case would be right-left+1+1.
Where righ-left would be the length from left to right, but without counting the left one. Hence one +1.
The other +1 is for making one additional character of space, most likely for adding a 0 termination.
By the way, the function is missing a clean return statement.
E.g. (thanks David C. Ranking for the idea of an example) "this" within "more this than that":
more this than that
0 5 8
The 't' is at left 5, the 's' is at right 8. The length of "this" is clearly 4 (not counting the, absent, 0 terminator). But 8-5 is only 3. So use one +1 to include the additional character (by my counting the 't' is missing...).
If that substring is to be returned via a pointer to malloced memory, which is what I guess the (missing) rest of the function is supposed to do; it should include the 0-terminator in the malloced memory. That needs another character of memory to be malloced; by using the second +1.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Below is taken from C Programming Language p. 117
find -nx pattern is the command with -nx and pattern arguments. When this command is invoked, the following main function is invoked. On the 1st iteration of the 2nd while loop, i can see char n is assigned to c, it then hits case n statement, then the 2nd iteration of the 2nd while loop, this 2nd iteration is supposed to assign char x to c, but i cannot see how that can be the case! For me, the 2nd iteration is still retrieving char n for the expression *++argv[0]
The pointer argv[0] was incremented in the 1st iteration of the loop. If the argument is "-nx", it points to 'x'.
The expression *++argv[0]
Accesses argv[0].
Increments the pointer stored there (++...).
Dereferences the character pointed to after the increment (*...).
Since this is really very old code, don't be tempted to learn from it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example, I have this string:
ABABcct
Can I copy
ABc
begin index = 2, end index = 4
...from that string?
Is there any specific built-in function that can do this?
I know strncpy() can copy a number of characters of a string starting from the beginning of the string onto another. But I don't think it can do what I have just described. As far as I know, its "begin index" is limited to 0.
Actually you can use the function strncpy() with a pointer to anywhere in your string, not only a pointer to its beginning.
Example:
char *src = strdup("ABABcct");
char *dest;
strncpy(dest, src + 2, 3) // Copies 3 characters from your pointer src + 2 to your pointer dest
If you run this code, dest will contain ABc.
While in your case the standard copy functions would eventually work, their use is not allowed when the source and the destination are overlapped, in which case you'll experience an UB.
For such issue the standard C library provide the function memmove designed to handle overlapped areas. It works as using an intermediate buffer to solve any overlapping problem.
see also Meaning of overlapping when using memcpy
With strncpy
#include <stdio.h>
#include <string.h>
int main(void)
{
char p[10],s[10]="abcdefgh";
strncpy(p,s+2,3);
puts(p);
return 0;
}
I know strncpy() ... As far as I know, its "begin index" (as I call it) is limited to 0.
You knew it wrong. It's not about begin index or anything - simply you need to pass relevant char* in src and target which is being done here.
To add further clarification - most standard library functions which you are talking about never put a constraint of passing the pointer to the 0-th index. It doesn't matter which index you send - you can send 100th or 50th doesn't matter as long it follows the things expected by function (null termination etc).
The code posted here is for illustration purpose - didn't include the necessary checks needed to use str*cpy functions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am wondering if it is possible to run same program twice, and every time it does something different. For instance I have two programs one that writes with fifo pipes and one that reads from it. So there are programA.c and programB.c (simple program, just sending some integers).
But I would like to run it like that:
./program & sleep 1; ./program
So one program would have two operation modes.
Thanks.
Yes, it is possible. You can run a program however many times you want. However, you may need to ensure that two instances of the same program do not contend for the same resources; for instance, if they both write to the same file, unexpected results may occur.
If you want the same program to do two different things (one writing to the fifo and one reading from it) you will have to ensure that the program can determine which action to take. One way of doing this would be to parse command-line parameters (e.g. invoke one as myprog --read and the other as myprog --write. Another would be for the program first to check for the existence of the fifo; if it doesn't exist, it could create the fifo and write to it, and if it does exist it could read from it.
Write your program to accept command line arguments like sed or awk.
Here is a simple example in C:
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 1 ) printf("You passed in zero arguments and your program is named %s\n", argv[0]);
if( argc > 1 ){
int x;
for(x = 1; x < argc; x++)
printf("Argument %d is named %s\n",x, argv[x]); //print multiple arguments
}
return 0;
}
If you compile and run this program with no arguments it will tell you that you didn't pass in any arguments; otherwise, you can pass in as many command line arguments as you like and it will print them back out for you.
The point is that this one program does different things depending on the arguments passed into it.