标签: character-arrays

在C++中比较字符数组和字符串文字

我有一个字符数组,我试图找出它是否匹配字符串文字,例如:

char value[] = "yes";
if(value == "yes") {
   // code block
} else {
   // code block
}
Run Code Online (Sandbox Code Playgroud)

这导致以下错误:与字符串文字比较导致未指定的行为.我也试过类似的东西:

char value[] = "yes";
if(strcmp(value, "yes")) {
   // code block
} else {
   // code block
}
Run Code Online (Sandbox Code Playgroud)

这不会产生任何编译器错误,但它的行为不符合预期.

c++ string-literals character-arrays

5
推荐指数
2
解决办法
1万
查看次数

为什么char*被视为与C中的char**相同​​?

我有以下测试应用程序:

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

int main(void){
    char buf[512];
    buf[0]= 0x1;
    buf[1]= 0x2;
    char *temp1 = &buf;
    char *temp2 = buf;
    char *temp3 = &buf[0];
    printf("temp1:%p, temp2:%p, temp3:%p\n",temp1,temp2,temp3);
    printf("0 = %d, %d, %d\n",temp1[0],temp2[0],temp3[0]);
    printf("1 = %d, %d, %d\n",temp1[1],temp2[1],temp3[1]);
    return;
}
Run Code Online (Sandbox Code Playgroud)

它汇编了一个警告:

gcc ./testptr.c -o testptr
./testptr.c: In function ‘main’:
./testptr.c:9: warning: initialization from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,所有三个指针的行为都相同.

./testptr
temp1:0x7fff3a85f220, temp2:0x7fff3a85f220, temp3:0x7fff3a85f220
0 = 1, 1, 1
1 = 2, 2, 2
Run Code Online (Sandbox Code Playgroud)

我知道buf == &buf[0],但为什么&buf == …

c arrays pointers character-arrays

5
推荐指数
1
解决办法
459
查看次数

const char*和free()

鉴于下一个代码示例,我无法释放参数const char* expression:

// removes whitespace from a characterarray
char* removewhitespace(const char* expression, int length)
{
    int i = 0, j = 0;
    char* filtered;
    filtered = (char*)malloc(sizeof(char) * length);
    while(*(expression + i) != '\0')
    {
        if(!(*(expression + i) == ' '))
        {
            *(filtered + j) = *(expression + i);
            j++;
        }
        i++;
    }
    filtered[j] = '\0';
    free(expression); //this doesn't seem to work
    return filtered;
}
Run Code Online (Sandbox Code Playgroud)

在我返回此函数之前,我尝试释放表达式参数中的数据,但我似乎无法释放它.
我认为这可能是因为它是一个常数,但我了解到C中的字符数组总是应该是常量.

我得到的错误消息是在行,free(expression)消息是:
expected void* but argument is of type …

c free memory-management character-arrays

5
推荐指数
1
解决办法
1万
查看次数

程序跳过cin.getline()

我制作了这个程序,它得到了用户的地址,名称和工作.然后它将所有内容放入一个字符串并输出该字符串.(我知道有更好的方法可以做到这一点)

char str[600];
char adrs[200];
char name[10];
char wrk[200];
cout<<"Enter your name and press ENTER: ";
cin.getline(name,10);
cout<<"\nEnter your adress and press ENTER:";
cin.getline(adrs,200);
cout<<"\nEnter your workplace and press ENTER:";
cin.getline(wrk,200);
strcpy(str,"My name is ");
strcat(str,name);
strcat(str,"\nand i live at ");
strcat(str,adrs);
strcat(str, "\nI also work at ");
strcat(str, wrk); strcat(str, "\n\n");
cout<<str<<endl;
Run Code Online (Sandbox Code Playgroud)

在这里,当我写一个超过10个字符的名称时程序确实采用了用户输入的前9个字符,但是之后它会跳过程序中的所有下一个字符cin.getline()并转到输出str并结束程序.

为什么会发生这种情况以及如何解决?

c++ arrays string character-arrays

5
推荐指数
1
解决办法
7417
查看次数

将字符数组的内容复制到Qt中的QString

我有一个字符指针,在任何运行中可以有不同的长度.例如:

char*  myChar;
Run Code Online (Sandbox Code Playgroud)

在一次运行中,其内容可以是"Hi",而在另一次运行中,它可以是"Bye".

我想将内容复制myChar到QString,例如,如果我有:

QString myString;
Run Code Online (Sandbox Code Playgroud)

我要复制内容myCharmyString; 我怎样才能做到这一点?

c++ qstring qt character-arrays

5
推荐指数
1
解决办法
2万
查看次数

如何作为MATLAB数组多次重复一个字符?

给定MATLAB字符数组中的单个字符串值:

['12 N']
Run Code Online (Sandbox Code Playgroud)

如何在新的字符数组中重复此值X次?

例如:

X = 5

['12 N'; '12 N'; '12 N'; '12 N'; '12 N']
Run Code Online (Sandbox Code Playgroud)

arrays matlab repeat character-arrays

5
推荐指数
1
解决办法
2万
查看次数

在 Fortran 中分配字符数组

我必须编写一个可以由 C 和 Fortran 调用的子例程。该子例程将文件名作为其参数之一。我知道为了与 C 良好地互操作,ISO C 绑定建议使用字符数组进行互操作。

我的问题是:是否存在易于编写的字符数组文字之类的东西?我有一个这样的子程序:

subroutine my_sub(char_array)
  use iso_c_binding, only: c_char
  char(kind=c_char, len=1), dimension(:), intent(in) :: char_array
  ...
end subroutine my_sub
Run Code Online (Sandbox Code Playgroud)

是否可以通过以下方式调用它:

call my_sub('Hello World!')
Run Code Online (Sandbox Code Playgroud)

或者我必须做一些可怕的事情,比如:

call my_sub((/ 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' /))
Run Code Online (Sandbox Code Playgroud)

我的主要问题似乎是它不喜欢假定形状的数组,并且给它一个设定的(大)大小也会输出随后拾取的所有垃圾内存。

有更好的方法吗?

string fortran character-arrays fortran-iso-c-binding

5
推荐指数
1
解决办法
6869
查看次数

scanf(),sscanf()或fscanf()中的%[]或%[^]说明符是否将输入存储在以null结尾的字符数组中?

以下是Beez C指南 (LINK)讲述%[]格式说明符的内容:

It allows you to specify a set of characters to be stored away (likely in an array of chars). Conversion stops when a character that is not in the set is matched.

如果您能澄清一些基于这个前提的基本问题,我将不胜感激:

1)存储在参数(类型char*)中的那两个格式说明符作为字符数组或带有\0终止字符(字符串)的字符数组获取输入吗?如果不是字符串,如何将其存储为字符串,在下面的程序中我们想要将字符序列作为字符串获取并在遇到特定字符(在否定字符集中)时停止?

2)我的程序似乎建议%[^|]|遇到否定字符时处理停止为说明符.但是当它再次为下一个格式说明符启动时,它是否从之前停止的否定字符开始?在我的程序中我打算忽略|因此我使用过%*c.但我测试并发现如果我使用%c和另一个类型的参数char,那么该字符|确实存储在该参数中.

3)最后但对我来说至关重要的是,为%s格式说明符传递字符数组printf()和字符串(NULL终止字符数组)之间的区别是什么?在我的另一个程序标题中character array vs string,我传递了一个字符数组(不是NULL终止) )对于%s格式说明符printf(),它就像字符串一样打印出来.有什么区别?

//用于说明%[^]说明符的程序

#include<stdio.h> …
Run Code Online (Sandbox Code Playgroud)

c string scanf format-specifiers character-arrays

4
推荐指数
1
解决办法
1万
查看次数

使用C指针与char数组

int i=512;
char *c = (char *)&i;
c[0] =1;
printf("%d",i);
Run Code Online (Sandbox Code Playgroud)

这显示"513",它为i加1.

int i=512;
char *c = (char *)&i;
c[1] =1;
printf("%d",i);
Run Code Online (Sandbox Code Playgroud)

而这显示256.除以2.有人可以解释为什么?非常感谢

c arrays pointers character-arrays

3
推荐指数
1
解决办法
2207
查看次数

使用引用指向Perl中的滑动窗口数组

这是我的问题:我有2个数组.一个是字符数组,表示一个滑动窗口.字符从头开始移动并在结尾处推送.我想使用第二个数组来存储对数组切片的引用,这些数组切片在它们移动时"跟随"字符.例:

my @char_array = ('h','e','l','l','o','w','o','r','l','d');
my $char_arr_ref=[@char_array[1..$#char_array]]; 
print @$char_arr_ref, "\n"; # slice contains 'elloworld';
shift(@char_array);
push(@char_array), 'x';
print @$char_arr_ref, "\n"; # slice still contains 'elloworld', not 'lloworldx' as I need;
Run Code Online (Sandbox Code Playgroud)

换句话说,我希望能够使用第二个数组引用数组切片(例如我在C中使用指针数组).

在Perl中有这样的惯用方法吗?

更新:这是进行快速文本搜索的大型程序的一部分.我打算使用一个引用的哈希(比如说,而不是'index'函数,它很慢.我需要在Perl中执行此操作.

perl reference slice character-arrays

3
推荐指数
1
解决办法
377
查看次数