用指针打印字符时出现问题

时间:2018-10-14 17:13:28

标签: c string pointers

我是初学者。我正在学习C。我通过Youtube了解了指针。然后,我想到了创建一个字符串数组。因此,我创建了一个程序。

#include <stdio.h>
#include <conio.h>

int main()
{
    char b[] = "Hi", *a;
    a = &b[0];
    printf("%s", a);
    getch();
    return 0;
}

当我在这里得到整个单词时。 a指向Hi的H。那么,代码中是否有任何错误?

1 个答案:

答案 0 :(得分:0)

字符数组的类型为checking examples ... ERROR Running examples in ‘smt-Ex.R’ failed The error most likely occurred in: > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: selComponentTracks > ### Title: selComponentTracks > ### Aliases: selComponentTracks > > ### ** Examples ... 349 lines ... + ## plot component tracks + #plotComponentTrackOverlay(folder=folder3,trackll.sel=trackll.sel) + + + + base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") + base::cat("selComponentTracks-methods", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") Error: unexpected symbol in: "base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") base" Execution halted checking whether package ‘smt’ can be installed ... WARNING Found the following significant warnings: Warning: replacing previous import ‘stats::lag’ by ‘dplyr::lag’ when loading ‘smt’ See ‘/Users/sys/code/smt.Rcheck/00install.out’ for details. checking for unstated dependencies in examples ... WARNING Warning: parse error in file 'smt-Ex.R': 959:1: unexpected symbol 958: cleanEx() 959: nameEx ^ ,但是当您执行char *时,a = &b被解释为类型b。因此,请删除char (*)[]或将其写为&

现在您已将其更改为a = &b[0]-即使a = &b[0]指向数组{的第一个元素,printf语句也会打印整个字符串,即"Hi" {1}}作为a格式说明符,使用指向某个字符串的字符指针作为参数来打印整个字符串。

加号b不是标准编译器的一部分-您应该停止使用 Turbo

相关问题