2

I am trying to execute printf function, which is printf ("%*s%s", indent, "", *w)

I know how it works, but I couldn't get the right result, which is "true". This is what I display the variables in gdb. After line 31, I execute:

(gdb) n

and I expected "true" to be printed. Instead, I got nothing as you can see. Why is it behaving like this?

31              printf ("%*s%s", indent, "", *w);
6: **w = 116 't'
5: *w = 0x7ffffffd7eb0 "true"
4: w = (char **) 0x7ffffffd76b0
3: **(c->u.word) = 116 't'
2: *(c->u.word) = 0x7ffffffd7eb0 "true"
1: c->u.word = (char **) 0x7ffffffd76b0
(gdb) n
32              while (*++w) // if next character is not null, keep printing.
6: **w = 116 't'
5: *w = 0x7ffffffd7eb0 "true"
4: w = (char **) 0x7ffffffd76b0
3: **(c->u.word) = 116 't'
2: *(c->u.word) = 0x7ffffffd7eb0 "true"
1: c->u.word = (char **) 0x7ffffffd76b0

After I execute (gdb) n I expected the value of *w get printed out. However I got nothing.

Why is it?

Code

case SIMPLE_COMMAND:
      {
    char **w = c->u.word;
    printf ("%*s%s", indent, "", *w);
    printf ("%*s%s", indent, "", "true");
    while (*++w) // if next character is not null, keep printing.
      printf (" %s", *w); 
    break;
      }

The structure of c->u.word looks like this:

struct command
{
  int status;
  union
  {

    struct command *command[2];

    char **word;

    struct command *subshell_command;
  } u;
};

EDIT:Another issue detedted

printf ("%*s%s", indent, "", "true");
    //abort();
    while (*++w) // if next character is not null, keep printing.
      printf (" %s", *w); 

When I put abort(); after the printf function, it prints out the right output. However, when I decomment abort, it does not print out anything. Also, the line after printf function "while(*++w) causes infinite loop, and I believe that the condition evaluates *++w, and I think it evaluates ++w first and * next.

w = w+1; *w;

How do I get out of this loop?

4

3 回答 3

2

联机帮助页上的格式说明符很难阅读,我错过了“。” before: printf ("%*s%s", indent, "", *w);应该是printf ("%.*s%s", indent, "", *w);并且字符串可能应该以 '\n' 结尾,以防止它在 stdout 上保持缓冲(这就是中止“修复”它的原因)。

于 2013-10-07T19:48:17.540 回答
0

Your char** w means that w is a pointer to a character pointer, not to a character. So when you do while (*++w) you are not advancing over characters, but over whole strings (hoping that the last one is NULL). Typically you see code like this in main(int argc, char** argv) where the array of strings is terminated with a NULL pointer. Otherwise your loop will crash.

Also, do you actually mean to do while(*w++) so that you'll print the first one as well?

Answer to comment: suppose you have a main program main(int argc, char** argv) and you invoke the program with this command line:

myprogram one two three

then inside the program you will see that

argv[0] is "myprogram"
argv[1] is "one"
argv[2] is "two"
argv[3] is "three"
argv[4] == NULL  so it is a null pointer. It does not point to a string.

so if you have this loop

while(char* w = *argv++){
    printf("%s\n", w);
}

you should see

myprogram
one
two
three

The reason it stops is w picks up the null pointer, which is 0.

于 2013-10-07T19:39:27.743 回答
0

通常,当您使用它打印东西时,printf它只会被放置在缓冲区中。在刷新缓冲区之前,您实际上不会看到任何东西。您可以通过调用显式刷新缓冲区fflush(stdout),也可以更改缓冲模式。输出到终端时,大多数 C 库默认为行缓冲,每次输出 '\n' (换行符)时都会隐式刷新缓冲区,因此如果将 a 添加\n到要输出的字符串中,您可能会看到一些内容。

于 2013-10-07T22:50:44.163 回答