这为什么不打印字符串?

huangapple go评论70阅读模式
英文:

why does this not print the string?

问题

Here's the translated code without the comments:

#include <stdio.h>
#include <stdlib.h>

int main(){

  const char *adj[] = {"swag", "nice", "asparagus", "ugly"};

  for (int i=0; i < sizeof(adj) / sizeof(adj[0]); i++)
    printf("you are extremely %s\n", adj[i]);

}

I've also made a correction to the loop condition to properly calculate the number of elements in the adj array.

英文:
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

int main(){
  
  const char *adj[] = {&quot;swag&quot;,&quot;nice&quot;,&quot;asparagus&quot;,&quot;ugly&quot;};

  for (int i=0;i&lt;sizeof(adj); i++)
    printf(&quot;you are extremely %s\n&quot;, *adj[i]);

}

i'm actually new to C and i'm trying test programs to learn, in this case i was hoping for it to print the name a few times with all the adjectives, but it doesn't when using %s in the last print, if i use %c it will only print the first letter of the string

tried swapping %c and %s, but neither work, %c prints only the 1st letter, %s prints nothing

答案1

得分: 3

以下是您要翻译的内容:

起始 sizeof( adj ) 等于 4 * sizeof( char * ),其中 4 是初始值的数量。如果 sizeof( char * ) 等于例如 8,那么 for 循环实际上看起来像是

for (int i=0;i &lt; 32; i++)

而数组仅包含 4 个元素。

要获取数组中元素的数量,您需要使用表达式 sizeof( adj ) / sizeof( *adj ),这等同于 sizeof( adj ) / sizeof( char * )

由于该表达式的类型是 size_t,最好也将变量 i 声明为 size_t 类型

for ( size_t i = 0; i &lt; sizeof( adj ) / sizeof( *adj ); i++ )

printf 调用的参数应该是一个指向字符串的指针,而表达式 *adj[i] 的类型是 const char

因此,您需要编写如下代码

for ( size_t i = 0; i &lt; sizeof( adj ) / sizeof( *adj ); i++ )
{
    printf( "你非常%s\n", adj[i] );
}
英文:

For starters sizeof( adj ) is equal to 4 * sizeof( char * ) where 4 is the number of initializers. If sizeof( char * ) is equal to for example 8 then the for loop looks in fact like

for (int i=0;i &lt; 32; i++)

while the array contains only 4 elements.

To get the number of elements in the array you need to use the expression sizeof( adj ) / sizeof( *adj ) that is the same as sizeof( adj ) / sizeof( char * ).

And as the expression has the type size_t it is much better to declare the variable i also as having the type size_t

for ( size_t i = 0; i &lt; sizeof( adj ) / sizeof( *adj ); i++ )

And the argument of the call of printf shall be a pointer to a string while the expression *adj[i] has the type const char.

That is you need to write

for ( size_t i = 0; i &lt; sizeof( adj ) / sizeof( *adj ); i++ )
{
    printf( &quot;you are extremely %s\n&quot;, adj[i] );
}

答案2

得分: 2

以下是翻译好的部分:

#include<stdio.h>
#include<stdlib.h>

int main(){
  
  const char *adj[] = {"时髦","不错","芦笋","丑陋"};

  for (int i=0;i<sizeof(adj)/sizeof(*adj); i++)
    printf("你非常%s\n", adj[i]);
}

输出:

你非常时髦
你非常不错
你非常芦笋
你非常丑陋

sizeof(adj) 返回指针的总大小。有4个指针,每个是32位或64位,所以要通过sizeof(adj)/sizeof(*adj)来获取元素的数量,即总大小(以字节为单位)除以一个指针的大小。

代码中的第二个错误在于printf,你需要打印由char指针指向的字符串,而不是单个字符。

英文:

Here is the correct way to do it

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

int main(){
  
  const char *adj[] = {&quot;swag&quot;,&quot;nice&quot;,&quot;asparagus&quot;,&quot;ugly&quot;};

  for (int i=0;i&lt;sizeof(adj)/sizeof(*adj); i++)
    printf(&quot;you are extremely %s\n&quot;, adj[i]);
}

outputs

you are extremely swag
you are extremely nice
you are extremely asparagus
you are extremely ugly

sizeof(adj) returns the total size of the pointers. There are 4 pointers each is of 32bit or 64bit, so the number of elements have to be obtained by sizeof(adj)/sizeof(*adj) i.e., total size in bytes/ size of one pointer.

The second error in your code is in the printf, you have to print the string pointed by the char pointer not the single char.

答案3

得分: 1

sizeof(adj)

或者简单地

sizeof adj

将会给您数组的字节大小。数组的字节大小不一定与数组中的元素数量相同。如果数组元素的大小大于一个字节,那么数组的字节大小将大于数组中的元素数量。

因此,为了确定数组中元素的数量,您必须将数组的总大小除以数组中单个元素的大小。您可以使用以下表达式来实现:

sizeof adj / sizeof *adj

基于上述原因,您应该将这一行

for (int i=0;i&lt;sizeof(adj); i++)

修改为:

for ( int i=0; i &lt; sizeof adj / sizeof *adj; i++ )

另一个问题是这一行

printf(&quot;you are extremely %s\n&quot;, *adj[i]);

是错误的。%s printf 格式说明符要求您将一个指向要打印的以 null 结尾的字符串的 char * 作为函数参数传递。但是,您并没有这样做。您实际上将要打印的第一个字符作为函数参数传递,它是 char 类型。因为您将 char 而不是 char * 作为函数参数传递给 printf,所以您的程序具有未定义的行为

因此,出于这个原因,您应该删除 *,使该行看起来像这样:

printf( &quot;you are extremely %s\n&quot;, adj[i] );
英文:

The expression

sizeof(adj)

or simply

sizeof adj

will give you the size of the array in bytes. The size of the array in bytes is not necessarily identical to the number of elements in the array. If the size of an element of the array is larger than a single byte, then the size of the array in bytes will be larger than the number of elements in the array.

Therefore, in order to determine the number of elements in an array, you must divide the total size of the array by the size of a single element of the array. You can do this using the following expression:

sizeof adj / sizeof *adj

For the reasons stated above, you should change the line

for (int i=0;i&lt;sizeof(adj); i++)

to:

for ( int i=0; i &lt; sizeof adj / sizeof *adj; i++ )

Another issue is that the line

printf(&quot;you are extremely %s\n&quot;, *adj[i]);

is wrong. The %s printf format specifier requires that you pass as a function argument a char * that points to the null-terminated string that is to be printed. However, that is not what you are doing. You are instead passing as the function argument the first character that is to be printed, which is of type char. Because you are passing a char instead of a char * as a function argument to printf, your program has undefined behavior.

For this reason, you should remove the *, so that the line looks like this:

printf( &quot;you are extremely %s\n&quot;, adj[i] );

huangapple
  • 本文由 发表于 2023年5月10日 16:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定