我需要保存一个C字符串数组.现在我知道C字符串只是一个字符数组,所以基本上我想要的是一个二维数组的字符.我想要存储的字符串也不会超过6个字符.我的计划是使用50个"字符串槽"初始化一个char数组,然后如果我点击50个字符串,则重新分配该数组的内存以使其容量加倍.我尝试过一些简单的事情:
int main() {
char strings[50][6];
strings[0] = "test";
printf("The string is: %s", strings[0]);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我去编译它时,我收到以下错误:
test.c:在函数'main'中:test.c:3:错误:从类型'char*'test.c中分配类型'char [6]'时出现不兼容的类型:4:警告:不兼容的内置隐式声明在函数'printf'中
谁能指出我正确的方向?
我是C++的新手,我有很多Objective-C经验.
我正在尝试将一个c字符串数组(即char **)作为我的类中的实例变量,它在我的构造函数中被分配和填充,然后在另一个成员函数中我要打印出整个"网格".
分配工作,我用字符串填充我的数组(现在只是"aaaaaaa"等等).检查我的构造函数的末尾,我看到已成功创建并按预期填充每一行.
但是,然后我调用了我的printGrid()函数,然后事情变得奇怪了.如果我要打印25行,比如说,前12个左右会打印垃圾,剩下的13个打印出来就像预期的那样.所以我似乎在某处践踏记忆,我不确定在哪里.
我的代码可能看起来有点凌乱,因为我一直在尝试不同的东西,所以我会尽量让它看起来尽可能具有凝聚力.
main.cpp:我在哪里调用函数
#include <iostream>
#include "Bitmap.h"
using namespace std;
int main (int argc, char * const argv[]) {
Bitmap bitmap(15, 25);
bitmap.printBitmap();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Bitmap.h:我班级的标题
class Bitmap {
private:
char **_bitmap;
void printLine(char const*lineString);
int _width;
int _height;
public:
Bitmap();
Bitmap(int width, int height);
void printBitmap();
};
Run Code Online (Sandbox Code Playgroud)
Bitmap.cpp:动作发生的位置
#include <iostream>
#include "Bitmap.h"
using namespace std;
Bitmap::Bitmap() {
// allocate space for the bitmap
int numRows = 20;
int numColumns = 30;
Bitmap(numRows, …Run Code Online (Sandbox Code Playgroud) 以下是C++.我有一个字符串,其中包含我需要在每个变量的声明中将其拆分并将其存储在字符串中的环境变量:
char* envVars = "=::=::\0system=blah\0othervar=blah\0"
Run Code Online (Sandbox Code Playgroud)
所以我使用cstring函数在空终止符char'\ 0'出现时拆分字符串,但它只是进入无限循环.为什么?
解决方案找到:查看代码注释:
vector <string> GetEvironmentVariables()
{
vector <string> envVariables;
char* environVar = GetEnvironmentStrings();
char* pos = strchr( environVar, '\0' );
// As far as I know environVar =::=::\0environVar1=...\0environVar2=...\0"
// so the string has many NULL terminators
while ( pos != NULL )
{
char* buffer;
strncpy( buffer, environVar, strlen(pos) ); // on the 1st iteration: buffer SHOULD = "=::=::\0", 2nd buffer SHOULD = "environVar=...\0"
envVariables.push_back( string(buffer) );
environVar = pos; // SOLUTUION: I need …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个md5哈希,我正在与php md5哈希进行比较.
这两个不接缝是一样的
下面是我的c代码以及php compairison
为什么两个md5不一样?
做命令
gcc -Wall -lssl -o test test.c
Run Code Online (Sandbox Code Playgroud)
代码test.c
#include <stdio.h>
#include <openssl/md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include <time.h>
unsigned char result[MD5_DIGEST_LENGTH];
// Print the MD5 sum as hex-digits.
void print_md5_sum(unsigned char* md, char* md5) {
int i;
for(i=0; i < MD5_DIGEST_LENGTH; i++) {
char temp[3];
snprintf(temp,sizeof(temp),"%02x",md[i]);
if(i == 0){
strncpy(md5,temp,3);
}else{
strncat(md5,temp,MD5_DIGEST_LENGTH);
}
}
printf("md5 is %s \n", md5);
}
int main(int argc, char** argv ){
char* file_buffer = "testtest"; …Run Code Online (Sandbox Code Playgroud) 我一直在使用C++字符串并尝试使用C函数来加载char *字符串.由于需要作为一个参数,我一定要投它该是这样的:std::stringstrcpy()strcpy()char *
std::string destination;
unsigned char *source;
strcpy((char*)destination.c_str(), (char*)source);
Run Code Online (Sandbox Code Playgroud)
代码工作正常,当我在调试器中运行程序时,*source存储的值destination,但由于某些奇怪的原因,它不会打印出来的语句
std::cout << destination;
我注意到,如果我使用
std::cout << destination.c_str();
该值打印正确,一切都很好.为什么会这样?有没有更好的方法复制unsigned char*或char*进入std::string(stringstreams?)这似乎只有foo.c_str()在复制操作中指定字符串时才会发生.
编辑:要回答"你为什么要这样做?"的问题,我用的strcpy()是一个简单的例子.还有一些时候它比分配更复杂.例如,必须使用strncpy()或std::string从C库char *中将函数传递给函数来将X量的字符串A复制到字符串B中,该库作为缓冲区的参数.
有人可以解释一下为什么我会得到"分段错误......"以及如何在这段代码上修复它?
#include<stdio.h>
int str_length(char *s) {
int length = 0, i;
for(i = 0; *s; i++) {
s++;
}
return i;
}
char *strdel(char *s, int pos, int n) {
int i;
char *p, str[] = "";
p = str;
for(i = 0; i < str_length(s) - n + 1; i++) {
if(i >= pos) {
*(p + i) = *(s + i + n);
}
else {
*(p + i) = *(s + i);
}
}
s = str; …Run Code Online (Sandbox Code Playgroud) 我想知道为什么将字符串转换为char*似乎使新的char*不等于它来自的字符串.
如果我有:
//raw versions of the string:
string s = "fun";
char* c = "fun";
char* s_convert = strdup(s.c_str()); //converting the string to char*
printf("(string) == 'fun' -> %d\n", (s == "fun"));
printf("(char*) == 'fun' -> %d\n", (c == "fun"));
printf("(char* convert) == 'fun' -> %d\n", (s_convert == "fun"));
printf("(string) == (char*) -> %d\n", (s == c)); //does new char* equal original string
Run Code Online (Sandbox Code Playgroud)
生产:
(string) == 'fun' -> 1 //true
(char*) == 'fun' -> 1 //true
(char* convert) == 'fun' …Run Code Online (Sandbox Code Playgroud) 因此,此代码用于以任何随机顺序输入命令输入,它将返回输入后的值.Amt_Range是一个数字检查功能.
为什么这样做.它应该能够由于指针的比较.
更重要的是string()做了什么.根据我的有限理解,比较不应该起作用,因为交流样式字符串的形式为' - ''t'.提前致谢!!
int main(int argc, const char *argv[]) {
string dummy;
int tests = 0, quizzes = 0, assignments = 0, labs = 0, fin = 0;
int testweight = 0, quizweight = 0, assignweight = 0, labweight = 0, finweight = 0;
int counter = 1;
if (argv[counter] == string("-t")) {
dummy = argv[counter + 1];
tests = Amt_Range(dummy, "tests");
counter+=2;
} else if (argv[counter] == string("-q")) {
dummy = argv[counter + 1];
quizzes = Amt_Range(dummy, …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
void printArray(int* arr, int size) {
cout << "Printing the array..." << endl;
for (int index = 0; index < size; index++) {
cout << arr[index] << endl;
}
}
void populateArray(int* arr, int size) {
for (int index = 0; index < size; index++) {
arr[index] = index * 10 + 1;
}
}
int main() {
int size = 2;
int* arr = new int[size];
populateArray(arr, size);
size_t newSize …Run Code Online (Sandbox Code Playgroud)