标签: c-strings

字符串和char数组如何在C中工作?

我见过的导游似乎没有解释得这么好.

我的意思是,你可以为a分配内存char*,或者char[25]改为编写内存?有什么不同?然后有文字,不能被操纵?如果要将固定字符串分配给变量,该怎么办?就像那样,stringVariable = "thisIsALiteral"之后你如何操纵呢?

有人可以直接在这里设置记录吗?在最后一种情况下,使用文字,你如何处理空终止?我觉得这很混乱.


编辑:真正的问题似乎是,据我所知,你必须兼顾这些不同的结构,以完成甚至简单的事情.例如,只能char *作为参数或返回值传递,但只能char[]分配文字和修改.我觉得很明显,我们经常/总是需要能够做到这两点,这就是我陷入困境的地方.

c arrays string c-strings

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

如何从C中的字符串中提取子字符串?

我尝试使用strncmp,但它只有在我给它一个特定数量的字节我想要提取时才有效.

char line[256] = This "is" an example. //I want to extract "is"
char line[256] = This is "also" an example. // I want to extract "also"
char line[256] = This is the final "example".  // I want to extract "example"
char substring[256]
Run Code Online (Sandbox Code Playgroud)

我如何提取""之间的所有元素?并把它放在变量substring?

c string c-strings

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

constexpr函数返回字符串文字

返回整数文字副本的函数

int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)

可以使用关键字轻松转换为普通的编译时表达式constexpr.

constexpr int number()
{ return 1; }
Run Code Online (Sandbox Code Playgroud)

但是,当涉及到字符串文字时,我会感到困惑.通常的方法是返回指向const char字符串文字的指针,

const char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)

但我认为仅仅改变"const" constexpr并不是我想要的(作为奖励,它还会产生编译器警告,使用gcc 4.7.1 将不推荐的从字符串常量转换为'char*')

constexpr char* hello()
{ return "hello world"; }
Run Code Online (Sandbox Code Playgroud)

有没有办法以hello()这样的方式实现调用在下面的示例中用常量表达式替换?

int main()
{
    std::cout << hello() << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ string c-strings constexpr c++11

7
推荐指数
1
解决办法
6831
查看次数

__int64到CString返回错误的值 - C++ MFC

我想将__int64变量转换为CString.代码就是这样

__int64 i64TotalGB;
CString totalSpace;
i64TotalGB = 150;
printf("disk space: %I64d GB\n", i64TotalGB);
totalSpace.Format(_T("%I64d", i64TotalGB));
printf("totalSpace contains: %s", totalSpace);
Run Code Online (Sandbox Code Playgroud)

第一个printf打印

"disk space: 150GB"
Run Code Online (Sandbox Code Playgroud)

它是正确的,但第二个printf打印随机高数字,如

"totalSpace contains: 298070026817519929"

我也尝试使用INT64变量而不是__int64变量,但结果是一样的.这可能是什么原因?

c++ printf mfc c-strings int64

7
推荐指数
2
解决办法
282
查看次数

如果我们打印一个包含“%s”的字符串,输出将是什么?

我想知道如果我们打印内容中包含“%s”的字符串会是什么结果?

我尝试将其打印为“ hi%s”。

char *ptr="hi%s";
printf(ptr);
Run Code Online (Sandbox Code Playgroud)

我希望输出为“ hi”。但我以“嗨,嗨,%s”的名字得到它。

“嗨,嗨,%s”

c printf c-strings undefined

7
推荐指数
3
解决办法
293
查看次数

strncmp(s1, s2, 0) 返回的是未定义的行为(即最后一个参数为零)吗?

从标准中尚不清楚strncmp(来自string.h

int strncmp(const char *s1, const char *s2, size_t n);
Run Code Online (Sandbox Code Playgroud)

n如果其第三个参数是则应返回0

根据C17标准草案,7.24.4.4:

strncmp函数比较不超过n字符(不比较空字符后面的字符)[...]。

strncmp函数返回一个大于、等于或小于零的整数,相应地,因为 指向的 [...] 数组s1大于、等于或小于 指向的 [...] 数组s2

应该strncmp(s1, s2, 0)返回什么?或者标准对strncmp最后一个论点的情况保持沉默0

我的直觉告诉我,0作为返回值最有意义:

  • 0是最“对称”的答案(负或正返回值意味着不对称并且与未进行比较不一致)。
  • 0与假设的模型一致0,直到发现差异、n进行比较或到达字符串末尾。

但上述推理是哲学性的。

似乎该标准在技术上并未声明有关此情况的任何内容。我认为如果这样会更好

  • 显式定义结果(例如0
  • 宣布其为非法。

对于它的价值,glibc给我0(没有警告或错误)一堆简单的测试用例,例如strncmp("abc", "def", 0)使用以下编译器标志:

-Wall -Wextra -std=c90 -pedantic
-Wall -Wextra -std=c17 …
Run Code Online (Sandbox Code Playgroud)

c c-strings language-lawyer strncmp

7
推荐指数
1
解决办法
191
查看次数

将数组中的字符附加到字符指针

好的,所以我是一个通常会编写 Java/C++ 的人,而且我刚刚开始编写 C。我目前正在编写一个词法分析器,我无法忍受字符串在 C 中的工作方式,因为我可以'不执行字符串算术。所以这是我的问题:

char* buffer = "";
char* line = "hello, world";

int i;
for (i = 0; i < strlen(line); i++) {
    buffer += line[i];
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 C 中做到这一点?由于上面的代码不是有效的 C,我该怎么做呢?基本上我在循环一个 string line,并且我试图将每个字符附加到bufferstring 。

c c-strings string-literals

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

JNI GetByteArrayElements () 错误

我是 JNI 的新手,所以我对 JNI 和英语都不熟悉。

我的JNI项目是一个简单的文件读写。在 Java 中读取文件并将字节数组传递给 C API,然后使用 C 将其写入文件。

我的源代码:

Java代码是:

public class FileIO {
   static {
      System.loadLibrary("FileIO");         
   }

   private native void writeFile(byte[] msg);

   public static void main(String[] args) throws IOException { 

      byte[] array = Files.readAllBytes(new File("PtFBWCTn.mp3").toPath());

      // System.out.println(array.length);
      new FileIO(). writeFile(array); 
   }
}
Run Code Online (Sandbox Code Playgroud)

C代码:

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

#include "HelloJNI.h"

JNIEXPORT void JNICALL Java_FileIO_ writeFile (JNIEnv *env, jobject job, jbyteArray array ){

    jsize num_bytes = (*env)->GetArrayLength(env, array);
    printf("Byte length : %d\n" , …
Run Code Online (Sandbox Code Playgroud)

c java java-native-interface c-strings

6
推荐指数
1
解决办法
8412
查看次数

为什么 ++str 和 str+1 有效而 str++ 无效?

我知道这里有一些关于 p++、++p 和 p+1 之间区别的解释,但我还不能清楚地理解它,尤其是当它不使用该函数时:

void replace(char * str, char c1, char c2){

    if (*str == '\0') {
        return;
    }else if (*str == c1) {
        printf("%c", c2);
    }
    else {
        printf("%c", *str);
    }

    replace(++str, c1, c2);
}
Run Code Online (Sandbox Code Playgroud)

当我这样做replace(++str, c1, c2);replace(str+1, c1, c2);它有效时,但replace(str++, c1, c2);没有。为什么?

c recursion c-strings post-increment function-declaration

6
推荐指数
1
解决办法
333
查看次数

如何用一个字符替换两个字符?

所以我试图cc&一个句子替换(例如:)"Hi this is Mark cc Alice"。到目前为止,我有这个代码,它替换了第一个c&但第二个c仍然存在。我将如何摆脱第二个c

int main() {
    char str[] = "Hi this is Mark cc Alice";
    int i = 0;
    while (str[i] != '\0') {
        if (str[i] == 'c' && str[i + 1] == 'c') {
            str[i] = '&';  
            //str[i + 1] = ' ';
        }
        i++;
    }
    printf("\n-------------------------------------");
    printf("\nString After Replacing 'cc' by '&'");
    printf("\n-------------------------------------\n");
    printf("%s\n",str);
    return str;
}
Run Code Online (Sandbox Code Playgroud)

输入是:

Hi this is …
Run Code Online (Sandbox Code Playgroud)

c loops c-strings char

6
推荐指数
1
解决办法
96
查看次数