如何在C中打印结构体中的三个字节

发布于 2024-10-20 22:29:47 字数 376 浏览 1 评论 0原文

我有以下 C 代码:

person->title

标题是一个三字节字符数组。我想做的就是打印这三个字节。我无法将字符串格式化程序与 printf 一起使用,因为没有空字节。

如何打印出来而不必复制它或向其中添加空字节?

提前致谢!

更新

事实证明它不是一个数组,只是三个相邻的字节。如果我尝试使用建议的代码进行编译:

person->title[0]

它会失败并出现错误:

test.c:46: error: subscripted value is neither array nor pointer.

I have the following C code:

person->title

The title is a three byte char array. All I want to do is print those three bytes. I can't use the string formatter with printf because there is no null byte.

How can I print this out without having to copy it or add a null byte to it?

Thanks in advance!

UPDATE

Turns out it's not an array, just three adjacent bytes. If I try to compile with the code suggested:

person->title[0]

It fails with the error:

test.c:46: error: subscripted value is neither array nor pointer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

破晓 2024-10-27 22:29:47
printf("%c%c%c",person->title[0],person->title[1],person->title[2]);
printf("%c%c%c",person->title[0],person->title[1],person->title[2]);
岁吢 2024-10-27 22:29:47

请尝试这个....

char *a = &(person->title);
printf("%c %c %c", *a, *(a+1) , *(a+2));

编辑

基于结构中的20个字段,它更简单,在结构的情况下会发生连续的内存分配,

例如:

struct test {
 // Suppose the elements of the struct are only title and name each having 3 bytes
}*person;

// The first 3 bytes are w.r.t title and the very next 3 is allocated to name, note for 
// struct it`s stored as contiguous memory allocation


// code can be rewritten as - to display title and name

char *a = &(person->title);
printf("Title : %c%c%c", *a, *(a+1), *(a+2));
printf("Name : %c%c%c", *(a+3), *(a+4), *(a+5));

Please try this....

char *a = &(person->title);
printf("%c %c %c", *a, *(a+1) , *(a+2));

EDIT

Based on 20 fields in the struct it`s much more simple mate, contiguous memory allocation takes place in case of struct

eg:

struct test {
 // Suppose the elements of the struct are only title and name each having 3 bytes
}*person;

// The first 3 bytes are w.r.t title and the very next 3 is allocated to name, note for 
// struct it`s stored as contiguous memory allocation


// code can be rewritten as - to display title and name

char *a = &(person->title);
printf("Title : %c%c%c", *a, *(a+1), *(a+2));
printf("Name : %c%c%c", *(a+3), *(a+4), *(a+5));
溺渁∝ 2024-10-27 22:29:47

您可以使用类似的内容:

printf("%c%c%c", person->title[0], person->title[1], person->title[2]);

如果您确定三个字节已填充。如果它可能短于三个字节,请尝试以下操作:

for (int i=0; (i<3) && (person->title[i] != 0); i++)
  printf("%c", person->title[i]);

You could use something like:

printf("%c%c%c", person->title[0], person->title[1], person->title[2]);

if you are sure the three bytes are filled. If it could be shorter than three bytes, try something like:

for (int i=0; (i<3) && (person->title[i] != 0); i++)
  printf("%c", person->title[i]);
夏末染殇 2024-10-27 22:29:47

对于结构 - 您可以利用内存将被分配为连续字节的事实。您还可以使用 fwrite() 来精确控制写入的字节数(如果您希望将其打印到终端,则使用“stdout”作为文件指针)。

例如

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

typedef struct _test
{
  char title[3];
  char job[4];
  char other[5];
} Test;

int main(int argc, char* argv)
{
  Test test;

  strcpy(test.title,"MR_");
  strcpy(test.job,"1234");
  strcpy(test.other,"OTHER"); 

  fwrite(&test,1,sizeof(Test),stdout);
  printf("\n");
}

将输出:

MR_1234OTHER

For structs - you could exploit the fact that the memory will be allocated as contiguous bytes. You could also use fwrite() to control exactly how many bytes are written (using "stdout" as file pointer if you want it printed to the terminal).

e.g.

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

typedef struct _test
{
  char title[3];
  char job[4];
  char other[5];
} Test;

int main(int argc, char* argv)
{
  Test test;

  strcpy(test.title,"MR_");
  strcpy(test.job,"1234");
  strcpy(test.other,"OTHER"); 

  fwrite(&test,1,sizeof(Test),stdout);
  printf("\n");
}

Will output:

MR_1234OTHER
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文