修改用C为const char * [英] Modify const char * in C
问题描述
我在练了采访。即时通讯目前停留在问题被逆转C.常量字符串我知道,既然是str2的常量,我可以修改的位置STR2点,而不是它的价值。我有以下称reverse_const功能。这将扭转地方为const char * str_const并打印。然而,当我尝试打印ST2从主要方法逆转后,该字符串不再转回。它像reverse_const()暂时改变STR2的存储位置。我在做什么错在这里?
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;void反转(字符* STR){
INT c_size = strlen的(STR);
字符* c_begin = str中,* c_end = STR +(c_size - 1); INT I;
对于(i = 0; I< c_size / 2;我++){
* ^ c_begin = * c_end;
* ^ c_end = * c_begin;
* ^ c_begin = * c_end; c_begin ++;
c_end--;
}
}无效reverse_const(为const char * str_const){
INT c_size = strlen的(str_const);
焦炭海峡[c_size]
的strcpy(STR,str_const);
字符* c_begin = str中,* c_end = STR +(c_size - 1); INT I;
对于(i = 0; I< c_size / 2;我++){
* ^ c_begin = * c_end;
* ^ c_end = * c_begin;
* ^ c_begin = * c_end; c_begin ++;
c_end--;
} str_const =海峡;
的printf(%S \\ n,str_const);
}诠释主(){
STR1的char [] =夺宝;
的char * str2的=肯德基;
的printf(测试:\\ nString 1 pre-逆转:%S \\ n,STR1);
反向(STR1);
的printf(字符串1逆转后:%S \\ n,STR1);
的printf(常量字符串2 pre-逆转:%S \\ n,STR2);
reverse_const(STR2);
的printf(常量字符串2逆转后:%S \\ n,STR2);
}
如果你想扭转 STR2
回的main()
,你要么需要一个足够大的缓冲区传递到 reverse_const
持有相反的字符串,或者您需要动态地分配它存储在 reverse_const
(局部变量长度数组不会做):
的#include<&stdlib.h中GT;
...
无效reverse_const(为const char ** str_const)
{
INT c_size = strlen的(* str_const);
字符*海峡=释放calloc(c_size + 1,sizeof的* STR);
的strcpy(STR,* str_const);
字符* c_begin = str中,* c_end = STR +(c_size - 1); INT I;
对于(i = 0; I< c_size / 2;我++){
* ^ c_begin = * c_end;
* ^ c_end = * c_begin;
* ^ c_begin = * c_end; c_begin ++;
c_end--;
} * str_const = str中;
的printf(%S \\ n,* str_const);
}诠释主要(无效){ STR1的char [] =夺宝;
的char * str2的=肯德基; 的printf(测试:\\ nString 1 pre-逆转:%S \\ n,STR1); 反向(STR1); 的printf(字符串1逆转后:%S \\ n,STR1);
的printf(常量字符串2 pre-逆转:%S \\ n,STR2); reverse_const((为const char **)及STR2); 的printf(常量字符串2逆转后:%S \\ n,STR2); 免费(STR2); 返回0;
}
输出
$ ./bin/revconststr
测试:
串1 pre-逆转:印第安纳
串1后的逆转:anaidnI
常量字符串2 pre-逆转:肯塔基州
ykcutneK
常量字符串2逆转后:ykcutneK
返回指针
您有更多的选择,指针返回 STR
分配到 STR2
在的main()
。这更是你通常会期望看到的。让我知道如果你有任何问题:
的char * reverse_const2(为const char ** str_const)
{
INT c_size = strlen的(* str_const);
字符*海峡=释放calloc(c_size + 1,sizeof的* STR);
的strcpy(STR,* str_const);
字符* c_begin = str中,* c_end = STR +(c_size - 1); INT I;
对于(i = 0; I< c_size / 2;我++){
* ^ c_begin = * c_end;
* ^ c_end = * c_begin;
* ^ c_begin = * c_end; c_begin ++;
c_end--;
} // * str_const = str中;
的printf(%S \\ n,* str_const); 返回海峡;
}INT主要(无效)
{ STR1的char [] =夺宝;
的char * str2的=肯德基; 的printf(测试:\\ nString 1 pre-逆转:%S \\ n,STR1); 反向(STR1); 的printf(字符串1逆转后:%S \\ n,STR1);
的printf(常量字符串2 pre-逆转:%S \\ n,STR2); STR2 = reverse_const2((为const char **)及STR2); 的printf(常量字符串2逆转后:%S \\ n,STR2); 免费(STR2); 返回0;
}
I am practicing for interviews. The problem Im currently stuck on is reversing a constant string in C. I know that since str2 is const, I can modify the location str2 points to, but not its value. I have a function below called reverse_const. It will reverse the const char *str_const in place and print it. However, when I try to print st2 after reversal from the main method, the string isn't reversed anymore. Its like reverse_const() is temporarily changing the memory location of str2. What am I doing wrong here?
#include <stdio.h>
#include <string.h>
void reverse(char *str){
int c_size = strlen(str);
char *c_begin = str, *c_end = str + (c_size - 1);
int i;
for(i = 0; i < c_size / 2; i++){
*c_begin ^= *c_end;
*c_end ^= *c_begin;
*c_begin ^= *c_end;
c_begin++;
c_end--;
}
}
void reverse_const(const char *str_const){
int c_size = strlen(str_const);
char str[c_size];
strcpy(str, str_const);
char *c_begin = str, *c_end = str + (c_size - 1);
int i;
for(i = 0; i < c_size / 2; i++){
*c_begin ^= *c_end;
*c_end ^= *c_begin;
*c_begin ^= *c_end;
c_begin++;
c_end--;
}
str_const = str;
printf("%s\n", str_const);
}
int main(){
char str1[] = "Indiana";
char *str2 = "Kentucky";
printf("TESTS:\nString 1 pre-reversal: %s\n", str1);
reverse(str1);
printf("String 1 post-reversal: %s\n", str1);
printf("Constant string 2 pre-reversal: %s\n", str2);
reverse_const(str2);
printf("Constant string 2 post-reversal: %s\n", str2);
}
If you want the reversed str2
back in main()
, you will either need to pass an adequately sized buffer to reverse_const
to hold the reversed string, or you will need to dynamically allocate storage for it in reverse_const
(a local variable length array won't do):
#include <stdlib.h>
...
void reverse_const (const char **str_const)
{
int c_size = strlen (*str_const);
char *str = calloc (c_size + 1, sizeof *str);
strcpy (str, *str_const);
char *c_begin = str, *c_end = str + (c_size - 1);
int i;
for (i = 0; i < c_size / 2; i++) {
*c_begin ^= *c_end;
*c_end ^= *c_begin;
*c_begin ^= *c_end;
c_begin++;
c_end--;
}
*str_const = str;
printf ("%s\n", *str_const);
}
int main (void) {
char str1[] = "Indiana";
char *str2 = "Kentucky";
printf ("TESTS:\nString 1 pre-reversal: %s\n", str1);
reverse (str1);
printf ("String 1 post-reversal: %s\n", str1);
printf ("Constant string 2 pre-reversal: %s\n", str2);
reverse_const ((const char **)&str2);
printf ("Constant string 2 post-reversal: %s\n", str2);
free (str2);
return 0;
}
Output
$ ./bin/revconststr
TESTS:
String 1 pre-reversal: Indiana
String 1 post-reversal: anaidnI
Constant string 2 pre-reversal: Kentucky
ykcutneK
Constant string 2 post-reversal: ykcutneK
Returning The Pointer
You have an additional option to return the pointer to str
to assign to str2
in main()
. This is more what you would normally expect to see. Let me know if you have any questions:
char *reverse_const2 (const char **str_const)
{
int c_size = strlen (*str_const);
char *str = calloc (c_size + 1, sizeof *str);
strcpy (str, *str_const);
char *c_begin = str, *c_end = str + (c_size - 1);
int i;
for (i = 0; i < c_size / 2; i++) {
*c_begin ^= *c_end;
*c_end ^= *c_begin;
*c_begin ^= *c_end;
c_begin++;
c_end--;
}
//*str_const = str;
printf ("%s\n", *str_const);
return str;
}
int main (void)
{
char str1[] = "Indiana";
char *str2 = "Kentucky";
printf ("TESTS:\nString 1 pre-reversal: %s\n", str1);
reverse (str1);
printf ("String 1 post-reversal: %s\n", str1);
printf ("Constant string 2 pre-reversal: %s\n", str2);
str2 = reverse_const2 ((const char **)&str2);
printf ("Constant string 2 post-reversal: %s\n", str2);
free (str2);
return 0;
}
这篇关于修改用C为const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!