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.
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.
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.
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
#include<stdio.h>
int main(int argc,char *argv[]) {
printf("%c",++**++argv);
return 0;
}
Suppose the command line arguments passed were:
./a.out one two three
The output is:
p
Can someone please explain me what is happening?
Start from the back of the ++**++argv expression:
argv starts off as a pointer to element zero, i.e. "./a.out" or ""
++argv is char** that points to string "one"
*++argv is char* that points to string "one"'s initial element
**++argv is char that is equal to string "one"s initial element, i.e. 'o'
++**++argv is char that follows 'o'. On most systems that's 'p'.
The last operation modifies program's arguments in place, which is allowed by the standard (Q&A).
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
The above expression will breakdown like *(&(&(&(*(*(*X)))))
As far as I could solve I think the compiler can trace the value at (***X). But what happens when it tries to retrieve the address of the value i.e'; &(***X). Will this work? Because the value is some random number is an expression. Will the compiler search for the value and return its address? If not what will be the output?
A compiler error. You can't dereference an int.
Your assumption is incorrect: *&&&***x will not break down like *(&(&(&(*(*(*X))))), it will be tokenized as * && & * * * x, resulting in a syntax error.
Adding spaces as in *& & &***x will result in a constraint violation because you cannot take the address of an address, nor can you dereference the int variable x as a pointer.
Conversely, *&*&*&*&x is a correct albeit contorted way to take the value of x, and so is 0[&x].
no, the problem occurs since x is of type int. it contains the value 2. when you say *x, it means "I want the value of the variable that x points to." and you know 2 is not a valid address in memory. this cause error.(the only valid address that you can assign is 0.)
also, since x is of type int, you can't dereference int.you will got a compile error.