Tag: 字符串

在C中将int转换为char

#include char* intToString(int N); int power(int N, int M) { int n = N; if (M ==0) return 1; int i; for( i = 1; i 1) { M /= 10;numberLen++; } int ar[numberLen]; char str[numberLen + 1]; int i; for(i = 0; i < numberLen; i++) { str[numberLen – i – 1] = (N%power(10,i+1))/(power(10,i)); […]

使用strcpy时出现分段错误?

我试图通过传递在编译时定义路径: -DDCROOTDEF='”/path/to/stuff”‘ 在编译行上。 然后我尝试在代码中使用它,如: char * ptr_path; strcpy(ptr_path, DCROOTDEF); strcat(ptr_path,”/MainCommons/CommonLib/fonts/Arial.ttf”); char *pftf=ptr_path; gdImageStringFT(pimg,brect,iclr,pftf,pts,ang,ixp,iyp, (char *)cbuf); 这给了我一个分段错误。 但是,如果我尝试先打印字符串: char * ptr_path; strcpy(ptr_path, DCROOTDEF); strcat(ptr_path,”/MainCommons/CommonLib/fonts/Arial.ttf”); char *pftf=ptr_path; printf(“%s\n”,pftf); gdImageStringFT(pimg,brect,iclr,pftf,pts,ang,ixp,iyp, (char *)cbuf); 它工作得很好。 我错过了char指针的复杂性吗? 谢谢

如何在C中声明一个常量2d字符串数组

我有以下数组结构: left , right , top top , left , right top , top , left 我想在C中声明它,我这样做了: char *movements[][] = {{“left”,”right”,”top”}, {“top”,”left”,”right”}, {“top”,”top”,”top”} } 但我收到此error : array has incomplete element type. 实现这一目标的正确方法是什么(声明和访问(打印))。我是否需要采用三维数组?

C – 将当前日期放在文件名中

我有4个值: A,B,C,D 。 在使用这些值进行一组计算之后,我希望我的代码将结果输出到ABCD_MM.DD.YY.txtforms的文件中,以跟踪它何时完成。 我不太确定在C中做到这一点的最佳方法。我有一个使用itoa()的“工作”版本,它不是标准的C函数,并且会在我的机器以外的其他机器上运行(并且已经消失)编译时 这是我执行此操作的代码,有人可以帮助更好(并且普遍接受)的方式吗? char数组name是使用全局范围定义的。 void setFileName(){ time_t now; struct tm *today; char date[9]; //get current date time(&now); today = localtime(&now); //print it in DD.MM.YY format. strftime(date, 15, “%d.%m.%Y”, today); char buff[20]; char vars[20]; //put together a string of the form: //”ABCD_DD.MM.YY.txt” strcpy(vars, itoa(A, buff, 20)); strcat(vars, itoa(B, buff, 20)); strcat(vars, itoa(C, buff, […]

strcmpfunction无法正常工作

我在结构books数组上有一个deletefunction 。 我正在传递一系列记录, author of book name of book和size of the list 。 现在这里给出了list[0].author和list[5].author和author all等于“Dan Brown”(同一个字符串) void delete(struct books *list,char author[],char name[],int n) { int i,a; a=strcmp(list[0].author,list[5].author); printf(“%d\n”,a); // prints 0 a=strcmp(list[0].author,author); printf(“%d\n”,a); // prints other than 0 } 为什么会这样? 这有什么不对?

在C中拆分带有多字符分隔符的char字符串

我想基于多字符分隔符拆分char *string 。 我知道strtok()用于分割字符串,但它适用于单字符分隔符。 我想基于诸如”abc”子字符串或任何其他子字符串来拆分char * string。 如何实现?

安全处理字符串变量

你好,我对C很陌生,简而言之,我在课堂作业中做了以下工作: foo (char *var) { printf(var); } 我被告知这是不好的做法和不安全但我的导师没有得到关于此的详细信息。 我假设如果var的字符串值可由用户控制,它可能用于执行缓冲区溢出? 我如何正确强化此代码? 我是否必须限制str长度? 干杯谢谢!

如何将元素从字符串数组传递给线程?

需要一些帮助来解决“将元素从字符串数组传递给线程”的问题。 我的代码是在这个文本之后。 我在main函数中声明了一个字符串数组,然后将一个数组元素传递给一个线程。 在线程中我将它转换回char *类型然后打印,但它打印垃圾值。 将不胜感激解决方案: #include #include void *agent(void *); int main(int argc, char *argv[]) { int i; pthread_t agent_t[3]; char *agent_colour[3] = {“Red”,”White”,”Brown”}; for(i = 0 ; i <= 2 ; i++) { pthread_create(&agent_t[i], 0, agent, &agent_colour[i]); } for(i = 0 ; i <= 2 ; i++) { pthread_join(agent_t[i], NULL); } return 0; […]

strcpy在ios​​7上表现不同

IOS7似乎带有字符串strcpy的新实现(可能是优化)。 之前我能够从数组的任何位置复制字符串,但现在如果我从任何位置开始复制(i%4!= 0)它将崩溃。 为了显示这一点,我在iOS6和7中都运行了这个代码,它在7上崩溃了应用程序: char *x = malloc(1024); strcpy(x, “hello world”); char *x2 = x + 1; strcpy(x, x2); 我究竟做错了什么?

如何使用宏将字符串转换为变量名?

#define TRACE(arg1,…) char* arg1; int main(void) { int a=4; TRACE((“Hello”,a)); // convert “Hello” to a valid char variable name. return 0; } 我在将字符串”Hello”转换为变量名时遇到了麻烦。 例如: “Hello”应该转换为const char* Hello; 通过使用宏。 由于有双引号,我无法转换它。 这是Stack Overflow中的第一个问题。