Tag: char

删除子字符串中给定位置的C中的子字符串

假设我有一个包含20个字符的char *example 。 我想删除example[5]到example[10]中的每个字符,然后修复数组,以便example[11]紧跟在example[4] 。 基本上将删除区域后的所有字符移动到删除区域开始时。 有任何想法吗? 编辑:我认为使用memcpy可能有一种方法? 但我不知道该怎么做。

char指针或char变量的默认值是什么

下面是我试图打印char变量和指针的默认值/值的代码。 但无法在控制台上看到它。它是默认值还是只能读取ASCII范围。 #include int main() { char c, *cp; printf(“\nValue of char c:%c\n”, c); printf(“\nValue of char ptr:%c\n”, *cp); return 0; }

返回指向C中结构的指针

该程序返回一个指向结构的指针 。 当我打印内容时, 名称没有正确显示,其他两个变量正在正确打印。 可能是什么问题呢? 这是C中的代码 #include struct student { char name[20]; int marks; int rank; }; struct student stu; struct student *create(); void main() { struct student *ptr; ptr = create(); printf(“%s\t %d\t %d\t”,ptr->name,ptr->marks,ptr->rank); } struct student *create() { struct student stu = {“john”,98,9}; struct student *ptrr; ptrr = &stu; return ptrr; }

如何将字符值从结构写入串口并转换为整数值?

struct MemoryTag1; typedef struct MemoryTag1{ char a[8]= {‘+’,’0′,’2′,’6′,’.’,’5′,’EA’,’\r’}; // setpoint temperature value char b[8]= {‘-‘,’0′,’2′,’4′,’.’,’5′,’EB’,’\r’}; char c[6]= {‘+’,’0′,’2′,’0′,’EC’,’\r’}; }Memory1; //这是一个消息结构,我想通过串行接口(RS232)传输,然后转换为整数值。 请指导我。

Scanf解析字符串输入到字符数组

我想在2个独立的数组中解析用户输入(使用scanf)。 g ++编译没有错误,但我得到一个内存访问错误(核心转储)。 (德语:“Speicherzugriffsfehler(Speicherabzug geschrieben)”) char *top[10]; char *bottom[10]; for(i = 0; i < 5; i++){ printf("Karte %d: Obere Werte? ", i ); scanf( "%s", top[i] ); printf( "Karte %d: Untere Werte? ", i); scanf( "%s", bottom[i] ); } 这有什么问题? 我尝试使用带有temp-var的”stringcpy” (“stringcpy(top[i], temp)”) ,但它也不起作用。 有什么建议?

涉及char数组的C语句的含义

我正在研究一个项目的算法,我遇到了一些我认为可能有用的代码。 但是,当我尝试阅读代码时,我在理解代码中的语句时遇到了一些困难。 这是代码。 int firstWord[MAX_WORD_SIZE] = {0}, c = 0; while (word1[c] != ‘\0’) //word1 is char[] sent as a parameter { firstWord[word1[c]-‘a’]++; c++; } 我理解(我希望是正确的)第一行是设置我的最大大小的整数数组,并将元素初始化为零,同时使初始计数器值“c”为零。 我知道while循环遍历word1[]数组的所有字符,直到它到达最后的char ‘\0’ 我对第一行firstWord[word1[c]-‘a’]++;的行感到困惑firstWord[word1[c]-‘a’]++; word1[c]应该给一个char,那么-‘a’做什么呢? 这会将char转换为一个整数,允许您访问firstWord[]数组的元素并使用++递增吗? 如果是这样, word1[c]-‘a’给出了哪个元素或整数

如何在c中完美截断字符串?

我正在读取一个文件,其中每行超过63个字符,我希望字符被截断为63.但是,它无法截断从文件中读取的行。 在这个程序中,我们假设文件只有10行: #include #include int main(void) { char a[10][63]; char line[255]; int count = 0; //Open file FILE *fp; fp = fopen(“lines.dat”, “r”); //Read each line from file to the “line array” while(fgets(line, 255,fp) != NULL) { line[63] = ‘\0’; //copy the lines into “a array” char by char int x; for(x = 0; x […]

c如何将字符变量与字符串进行比较?

以下代码在C中完全没问题,但在C ++中没有。 在以下代码中, if语句始终为false。 C如何将字符变量与字符串进行比较 int main() { char ch=’a’; if(ch==”a”) printf(“confusion”); return 0; }

将char指针编写为文件问题的结构成员

我有一个struct成员作为char *并将其分配给结构初始化的字符串文字“John”,如下所示。 字符串打印精细与printf。 但是,如果我使用fwrite将此字符串写入文件,则在文件中我读回垃圾。 如果我使用char数组而不是char *(如图所示在结构中注释掉),并将其写入文件,我可以按预期读回文件中的字符串。 我无法理解这一点。 在两种情况下都不应该使用指针并将正确的字符串写入文件? 此外,如果我声明一个单独的char *并将其指向字符串文字并将其写入文件,我可以按预期读回。 非常感谢您提供任何解释方面的帮助。 (我在Windows上使用带有Mingw编译器的codeblocks IDE)。 更新:包括使用的实际代码 int main() { struct person { //char name[20]; char* name; int age; }; struct person p1 = {“John”, 25}; printf(“%s\n”, p1.name); FILE* fp = fopen(“test.txt”, “w”); fwrite(&p1, 1, 10, fp); return 0; }

是否应使用C中的文字初始化具有指定大小的静态声明字符数组?

例如, gcc编译好了…… char s[7] = “abc”; 但它给出了错误“赋值不兼容的类型”与… char s[7]; s = “abc”; 有什么不同?