C “字符串”;“串联”;a';对于';块
我试图在C语言中实现某种“连接”,在一个字符串中使用两个值 代码如下所示:C “字符串”;“串联”;a';对于';块,c,arrays,string,concatenation,C,Arrays,String,Concatenation,我试图在C语言中实现某种“连接”,在一个字符串中使用两个值 代码如下所示: #include <stdio.h> #define A "A" int main() { char *textArray[] = {"a", "b", "c"}; int intArray[] = {1, 2, 3}; int n; // count intArray[] lengh // example taken from the https://www.
#include <stdio.h>
#define A "A"
int main() {
char *textArray[] = {"a", "b", "c"};
int intArray[] = {1, 2, 3};
int n;
// count intArray[] lengh
// example taken from the https://www.sanfoundry.com/c-program-number-elements-array/
n = sizeof(intArray)/sizeof(int);
int i;
char *concat;
for (i=0; i<n; i++) {
// check if values are accessible
printf("TEST: Macro: %s, textArray's value: %s, intArray's value: %d\n", A, textArray[i], intArray[i]);
// making concatenation here - this will work
concat = "Macro: " A " textArray's value: '' intArray's value: ''";
// this will NOT work
// expected result == 'Macro: A textArray's value: a intArray's value: 1' etc
// concat = "Macro: " A " textArray's value: " textArray[i] " intArray's value: " intArray[i];
printf("%s\n", concat);
}
return 0;
}
但是如果我试图使用textArray[I]
,即:
...
// making concatenation here - this will work
// concat = "Macro: " A " textArray's value: '' intArray's value: ''";
// this will NOT work
// expected result == 'Macro: A textArray's value: a intArray's value: 1' etc
concat = "Macro: " A " textArray's value: " textArray[i] " intArray's value: " intArray[i];
printf("%s\n", concat);
...
我在编译过程中出错:
所以问题是:我做错了什么?实现我的目标的正确方法是什么
UPD:最终目标是将一个字符串传递给一个函数,如
mysql\u query()
,例如mysql\u query(conn,concat)
,其中concat
将包含一个值,如“INSERT INTO-INTO-TableName-VALUES('textValue',intValue')”
“Foo”宏“Bar”
其中宏
为“27”
在预处理器时展平为“Foo”“27”“Bar”
,然后由编译器进一步展平为“Foo27Bar”
。您必须使用标准字符串concat函数将其他值合并为新字符串。textary[i]
是运行时的,但您使用的字符串连接,即“string1”和“string2”
仅在编译器连接这些字符串时才有效
使用textary[i]
需要编译器插入代码来计算i
和textary[i]
中的字符或字符串,然后使用代码来连接字符串。字符串“string”string1“string2”不是连接。它只是字符串文字的语法正确形式之一 感谢您的评论-我使用sprintf()
找到了解决方案
因此,我的代码(另一个“原始”代码,具有相同的想法)现在看起来像:
...
char *textArray[] = {"a", "b", "c"};
int intArray[] = {1, 2, 3};
int n;
// count intArray[] lengh
// example taken from the https://www.sanfoundry.com/c-program-number-elements-array/
n = sizeof(intArray)/sizeof(int);
int i;
for (i=0; i<n; i++) {
// best to check needed size for malloc() using sizeof()
// saving a query string into the `buffer` var
sprintf(buffer, "INSERT INTO %s VALUES(NULL, '%s', '%d')" , DB_TABLE, textArray[i], intArray[i]);
// pass connection obj + query string to the `mysql_query()`
mysqlexec(con, buffer);
}
...
现在所有工作:
检查结果:
为什么不
printf(“宏:%s%s:%d\n”,A,textary[i],intArray[i])
?@Someprogrammerdude在我的目标中添加了UPD。或者sprintf
如果你想将其存储在变量中,如果你需要一个字符串,那么,作为补充,如果你真的要将其传递给mysql\u query
,通常不会。改用参数绑定。连接会导致SQL注入攻击,嗯?源代码中的字符串文字由编译器连接成目标文件中的单个字符串文字。@PaulOgilvie编译器内部的操作并不重要。这一术语不应该被使用,因为它对许多人来说是非常混乱的——阅读本主题时你应该注意到这一点
$ gcc array_test.c -o array_test
array_test.c: In function ‘main’:
array_test.c:26:53: error: expected ‘;’ before ‘textArray’
concat = "Macro: " A " textArray's value: " textArray[i] " intArray's value: " intArray[i];
...
char *textArray[] = {"a", "b", "c"};
int intArray[] = {1, 2, 3};
int n;
// count intArray[] lengh
// example taken from the https://www.sanfoundry.com/c-program-number-elements-array/
n = sizeof(intArray)/sizeof(int);
int i;
for (i=0; i<n; i++) {
// best to check needed size for malloc() using sizeof()
// saving a query string into the `buffer` var
sprintf(buffer, "INSERT INTO %s VALUES(NULL, '%s', '%d')" , DB_TABLE, textArray[i], intArray[i]);
// pass connection obj + query string to the `mysql_query()`
mysqlexec(con, buffer);
}
...
void mysqlexec(MYSQL *con, char *query) {
printf("Running query: %s\n", query);
if (mysql_query(con, query)) {
finish_with_error(con);
}
}
$ ./get_id
Running query: INSERT INTO ExampleTable VALUES(NULL, 'a', '1')
Running query: INSERT INTO ExampleTable VALUES(NULL, 'b', '2')
Running query: INSERT INTO ExampleTable VALUES(NULL, 'c', '3')
The last inserted row id is: 3
MariaDB [testdb]> select * from ExampleTable;
+----+---------+--------+
| Id | TextCol | IntCol |
+----+---------+--------+
| 1 | a | 1 |
| 2 | b | 2 |
| 3 | c | 3 |
+----+---------+--------+