Why does the printf of ++**++argv output 'p'? [closed] - c

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).

Related

assign zero to item in char array [closed]

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 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).

iterating through argv pointer to char pointers [closed]

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.

C programming read a integer from command line input string [closed]

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
I just started programming and have a question. I have a example input from command line: "number:10" which is "number:" followed by a number. I want to check if the character after "number:" is a number:
int main(int argc, char **argv)
{
if(isdigit(*argv[2]+7)){
printf("correct");
}
return 0;
}
It doesn't work. How can I read only the number in the input string?
*argv[2] is 'n'. *(argv[2]+7) is correct

Differences between scanf and getchar in C [closed]

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 7 years ago.
Improve this question
I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf completes, x contains the character that was read. However, that value is immediately overwritten when x is assigned the return value of scanf, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf without assigning the return value to x you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);

A function to count how many different characters are in a string [closed]

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 8 years ago.
Improve this question
I've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false

Resources