格式'%s'的预期类型“的char *”的说法 [英] format ’%s’ expects argument of type ’char *’

查看:188
本文介绍了格式'%s'的预期类型“的char *”的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关行使我的编程技术用C我想由我自己写的函数strncpy功能。这样做,我不停地击球失误,解决他们大多最终我坚持,没有更多的灵感去。

For exercising my programming skills in C I'm trying to write the strncpy function by myself. Doing that I kept hitting errors, solving most of them eventually I'm stuck with no further inspiration to go on.

我收到的错误是:

ex2-1.c:29:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   printf("The copied string is: %s.\n", stringb);

的事情是,这是一个非常常见的错误,而且它也已经在这样描述的,只有我似乎无法申请其他人已经指出的提示。我得到打印变量时,我使用了错误的类型,当我使用%D格式它将返回一个整数,这可能是第一个字符的ASCII值,为增大最大数量时,它不会改变字节的复制。

The thing is that it's a very common error and that it's also already described on SO, only I can't seem to apply the tips other people have already pointed out. I get that I'm using a wrong type when printing the variable, and when I use the %d format it will return an integer which is probably the ASCII value of the first character, as it doesn't change when increasing the max number of bytes to copy.

使用GDB我发现,做while循环迭代当B变量包含正确的字符串,我仍似乎无法打印。

Using GDB I've found out that the b variable when done iterating through the while loop holds the correct string, still I can't seem to print it.

我可能缺乏对C语言的知识很基本的一部分,我的问这个问题的新手(再次)道歉。另外我想AP preciate如果你能提供反馈或在我的code指出其他缺陷。

I'm probably lacking a very fundamental part of knowledge about the C language and I apologise for asking this novice question (once again). Also I would appreciate it if you could give feedback or point out other flaws in my code.

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

void strmycpy(char **a, char *b, int maxbytes) {
  int i = 0;
  char x = 0;

  while(i!=maxbytes) {
  x = a[0][i];
  b[i] = x;
  i++;
  }

  b[i] = 0;

}


int main (int argc, char **argv) {
  int maxbytes = atoi(argv[2]);
  //char stringa;
  char stringb;
  if (argc!=3 || maxbytes<1) {
        printf("Usage: strmycpy <input string> <numberofbytes>. Maxbytes has to be more than or equal to 1 and keep in mind for the NULL byte (/0).\n");
        exit(0);
     } else {

  strmycpy(&argv[1], &stringb, maxbytes);
  printf("The copied string is: %s.\n", stringb);

  }

  return 0;
}


推荐答案

没有字符的char * 。首先是单个字符,而后者是一个指向字符(它可以指向字符的对象变量数)。

There is a slight difference between char and char*. The first is a single character whereas the later is a pointer to char (which can point to variable number of char objects).

%S 格式说明确实需要一个C风格的字符串,它不仅应该是类型的char * 但预计也将是空终止(见 C字符串处理)。如果您想打印一个字符,然后用%C 代替。

The %s format specifier really expects a C-style string, which should not only be of type char* but is also expected to be null-terminated (see C string handling). If you want to print a single character, then use %c instead.

至于程序,假设我想你想要的是你想要什么,尝试这样的:

As for the program, assuming that what I think you want is what you want, try something like this:

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

static void strmycpy(char *dest, const char *src, size_t n) {
    char c;
    while (n-- > 0) {
        c = *src++;
        *dest++ = c;
        if (c == '\0') {
            while (n-- > 0)
                *dest++ = '\0';
            break;
        }
    }
}

int main(int argc, char *argv[]) {
    size_t maxbytes;
    char *stringb;

    if (argc != 3 || !(maxbytes = atoll(argv[2]))) {
        fprintf(
            stderr,
            "Usage: strmycpy <input string> <numberofbytes>.\n"
            "Maxbytes has to be more than or equal to 1 and keep "
            "in mind for the null byte (\\0).\n"
        );
        return EXIT_FAILURE;
    }

    assert(maxbytes > 0);
    if (!(stringb = malloc(maxbytes))) {
        fprintf(stderr, "Sorry, out of memory\n");
        return EXIT_FAILURE;
    }

    strmycpy(stringb, argv[1], maxbytes);
    printf("The copied string is: %.*s\n", (int)maxbytes, stringb);
    free(stringb);

    return EXIT_SUCCESS;
}

但坦率地说,这是很基本的解释可能只是导致在C.写一本书,这样你会好很多了,如果你只是看已经写好之一。对于良好的C语言的书籍和资源列​​表,请参阅权威本C语言书中指南和列表

希望它帮助。祝你好运!

Hope it helps. Good Luck!

这篇关于格式'%s'的预期类型“的char *”的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆