如何在 C 中进行 char* 数组元素交换?
How to do char* array element swapping in C?
#include <stdio.h>
#include <stdlib.h>
char* temp=" ";
char char_array[4][100]= {"-11111","-1111","-1110","-1112"};
void str_swap(char** a,char** b);
int main(int argc,char* argv[]) {
int j=0,n=4;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0],&char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char** str1,char** str2) {
temp = *str1;
*str1 = *str2;
*str2 = temp;
}
你好all:I想在C中做char*数组元素交换,上面是我的代码。但是我没有改变下面的输出shown.Am我在这里遗漏了什么?谢谢你的帮助提前。
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER
str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
//should be -1110
char_array[1] = -1111
char_array[2] =
-1110 //should be -11111
char_array[3] = -1112
进程返回 0 (0x0) 执行时间:0.359 秒
按任意键
继续。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1002
char *char_array[MAXLEN];
void str_swap(char const** str1,char const** str2);
int my_compare_func(int,int);
void bubble_sort(int,int(*compare_func)(int,int));
int main(int argc,char* argv[]) {
int n=0,i=0,j=0;
while(scanf("%d",&n)==1) {
i=n;
while(i--) {
scanf("%s",&char_array[i]);
}
printf("BEFORE BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
bubble_sort(n,my_compare_func);
printf("AFTER BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
system("PAUSE");
}
return 0;
}
int my_compare_func(int x,int y) {
int i=0,res = 0,len_strx=0,len_stry=0;
len_strx = strlen(char_array[x]);
len_stry = strlen(char_array[y]);
if(char_array[x][0]=='-') {
if(char_array[y][0]=='-') {
if(len_strx>len_stry) {
res = -1;
}else if(len_strx<len_stry) {
res = 1;
}else if(len_strx==len_stry) {
i=1;
while((char_array[x][i]==char_array[y][i])&&(char_array[x][i]!='[=11=]')&&(char_array[y][i]!='[=11=]')) {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = 1;
}else if(char_array[x][i]>char_array[y][i]) {
res = -1;
}
}
}else if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
res = -1;
}
}else if((char_array[x][0]>='0'&&char_array[x][0]<='9')||char_array[x][0]=='+') {
if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
if(len_strx>len_stry) {
res = 1;
}else if(len_strx<len_stry) {
res = -1;
}else if(len_strx==len_stry) {
i=0;
while(char_array[x][i]==char_array[y][i]&&char_array[x][i]!='[=11=]'&&char_array[y][i]!='[=11=]') {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = -1;
}else if(char_array[x][i]>char_array[y][i]){
res = 1;
}
}
}else if(char_array[y][0]=='-') {
res = 1;
}
}
return res;
}
void str_swap(char const** str1,char const** str2) {
char const* temp = *str1;
*str1 = *str2;
*str2 = temp ;
}
void bubble_sort(int n,int(*compare_func)(int x,int y)) {
int i=0,j=0,flag=1;
for(i=n-1; (i>0)&&(flag==1); --i) {
flag=0;
for(j=0; j<i; ++j) {
if(compare_func(j,j+1)>0) {
str_swap(&char_array[j],&char_array[j+1]);
flag = 1;
}
}
if(!flag) {
break;
}
}
}
Thank you for all the answers.Your response is very fast.In fact I am
trying to solve the basic online judge problem.Check it here:(in mandarin)
http://zerojudge.tw/ShowProblem?problemid=a528
Basically it's a sorting problem with string storing big number(both
positive and negative).Sorry for posting such huge amount lines of
code.fast.My new problem is that while I have my code running to the line:
//printf("char_array[%d] = %s\n",j,&char_array[j]);
printf("char_array[%d] = %s\n",j,char_array[j]);
then the console application crashed!I tried some approach like the
commented line(that is,set the %s part in printf() function to be
&char_array[j]).It works fine but contradicts my knowledge about arguments > of printf().Can anyone explain this for me?Thanks :)
您提供的代码无法编译,因为 &char_array[0]
的类型不是 str_swap
函数期望得到的 char**
。基本上你可以通过进行这些更改来实现你想要的:
#include <string.h>
char temp[100];
...
void str_swap(char* a,char* b);
...
str_swap(char_array[0],char_array[2]);//you don't need to use the & operator here
...
void str_swap(char* str1,char* str2)
{
strncpy(temp,str1,100);//copy the first to temp
strncpy(str1,str2,100);//copy the second to the first
strncpy(str2,temp,100);
}
是的,您在这里遗漏了一些东西。 char [4][100]
是数组数组,不是指针数组。你的函数需要指针的地址,而不是数组的地址(数组不是指针)。
将您的数组更改为 const char*
的数组(指向常量字符数据的指针),并且您的函数正确地将指针交换为该类型的指针将解决您的问题。
#include <stdio.h>
#include <stdlib.h>
char const *char_array[4] = {"-11111","-1111","-1110","-1112"};
void str_swap(char const ** str1,char const ** str2);
int main(int argc,char* argv[])
{
int j=0,n=4;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0], &char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char const ** str1, char const ** str2)
{
char const *temp = *str1;
*str1 = *str2;
*str2 = temp;
}
输出
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112
你的原始代码 运行s 是一个谜,因为我使用的 C 编译器都不允许你正在做的事情进行编译,更不用说 运行.
要编译代码,您必须将矩阵更改为指向文字 c 字符串的指针数组,因此
char char_array[4][100]= {"-11111","-1111","-1110","-1112"};
必须
char *char_array[4]= {"-11111","-1111","-1110","-1112"};
您的代码应该是
#include <stdio.h>
#include <stdlib.h>
char *char_array[]= {"-11111","-1111","-1110","-1112"};
#define ARRAY_SIZE sizeof(char_array)/sizeof(char_array[0])
void str_swap(char** a,char** b);
int main() {
size_t j=0;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<ARRAY_SIZE; j++) {
printf("char_array[%zu] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0],&char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<ARRAY_SIZE; j++) {
printf("char_array[%zu] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char** str1, char** str2) {
char* temp;
temp = *str1;
*str1 = *str2;
*str2 = temp;
}
输出将是
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112
#include <stdio.h>
#include <stdlib.h>
char* temp=" ";
char char_array[4][100]= {"-11111","-1111","-1110","-1112"};
void str_swap(char** a,char** b);
int main(int argc,char* argv[]) {
int j=0,n=4;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0],&char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char** str1,char** str2) {
temp = *str1;
*str1 = *str2;
*str2 = temp;
}
你好all:I想在C中做char*数组元素交换,上面是我的代码。但是我没有改变下面的输出shown.Am我在这里遗漏了什么?谢谢你的帮助提前。
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111 //should be -1110
char_array[1] = -1111
char_array[2] = -1110 //should be -11111
char_array[3] = -1112进程返回 0 (0x0) 执行时间:0.359 秒
按任意键 继续。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1002
char *char_array[MAXLEN];
void str_swap(char const** str1,char const** str2);
int my_compare_func(int,int);
void bubble_sort(int,int(*compare_func)(int,int));
int main(int argc,char* argv[]) {
int n=0,i=0,j=0;
while(scanf("%d",&n)==1) {
i=n;
while(i--) {
scanf("%s",&char_array[i]);
}
printf("BEFORE BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
bubble_sort(n,my_compare_func);
printf("AFTER BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
system("PAUSE");
}
return 0;
}
int my_compare_func(int x,int y) {
int i=0,res = 0,len_strx=0,len_stry=0;
len_strx = strlen(char_array[x]);
len_stry = strlen(char_array[y]);
if(char_array[x][0]=='-') {
if(char_array[y][0]=='-') {
if(len_strx>len_stry) {
res = -1;
}else if(len_strx<len_stry) {
res = 1;
}else if(len_strx==len_stry) {
i=1;
while((char_array[x][i]==char_array[y][i])&&(char_array[x][i]!='[=11=]')&&(char_array[y][i]!='[=11=]')) {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = 1;
}else if(char_array[x][i]>char_array[y][i]) {
res = -1;
}
}
}else if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
res = -1;
}
}else if((char_array[x][0]>='0'&&char_array[x][0]<='9')||char_array[x][0]=='+') {
if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
if(len_strx>len_stry) {
res = 1;
}else if(len_strx<len_stry) {
res = -1;
}else if(len_strx==len_stry) {
i=0;
while(char_array[x][i]==char_array[y][i]&&char_array[x][i]!='[=11=]'&&char_array[y][i]!='[=11=]') {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = -1;
}else if(char_array[x][i]>char_array[y][i]){
res = 1;
}
}
}else if(char_array[y][0]=='-') {
res = 1;
}
}
return res;
}
void str_swap(char const** str1,char const** str2) {
char const* temp = *str1;
*str1 = *str2;
*str2 = temp ;
}
void bubble_sort(int n,int(*compare_func)(int x,int y)) {
int i=0,j=0,flag=1;
for(i=n-1; (i>0)&&(flag==1); --i) {
flag=0;
for(j=0; j<i; ++j) {
if(compare_func(j,j+1)>0) {
str_swap(&char_array[j],&char_array[j+1]);
flag = 1;
}
}
if(!flag) {
break;
}
}
}
Thank you for all the answers.Your response is very fast.In fact I am trying to solve the basic online judge problem.Check it here:(in mandarin) http://zerojudge.tw/ShowProblem?problemid=a528
Basically it's a sorting problem with string storing big number(both positive and negative).Sorry for posting such huge amount lines of code.fast.My new problem is that while I have my code running to the line:
//printf("char_array[%d] = %s\n",j,&char_array[j]);
printf("char_array[%d] = %s\n",j,char_array[j]);
then the console application crashed!I tried some approach like the commented line(that is,set the %s part in printf() function to be &char_array[j]).It works fine but contradicts my knowledge about arguments > of printf().Can anyone explain this for me?Thanks :)
您提供的代码无法编译,因为 &char_array[0]
的类型不是 str_swap
函数期望得到的 char**
。基本上你可以通过进行这些更改来实现你想要的:
#include <string.h>
char temp[100];
...
void str_swap(char* a,char* b);
...
str_swap(char_array[0],char_array[2]);//you don't need to use the & operator here
...
void str_swap(char* str1,char* str2)
{
strncpy(temp,str1,100);//copy the first to temp
strncpy(str1,str2,100);//copy the second to the first
strncpy(str2,temp,100);
}
是的,您在这里遗漏了一些东西。 char [4][100]
是数组数组,不是指针数组。你的函数需要指针的地址,而不是数组的地址(数组不是指针)。
将您的数组更改为 const char*
的数组(指向常量字符数据的指针),并且您的函数正确地将指针交换为该类型的指针将解决您的问题。
#include <stdio.h>
#include <stdlib.h>
char const *char_array[4] = {"-11111","-1111","-1110","-1112"};
void str_swap(char const ** str1,char const ** str2);
int main(int argc,char* argv[])
{
int j=0,n=4;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0], &char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char const ** str1, char const ** str2)
{
char const *temp = *str1;
*str1 = *str2;
*str2 = temp;
}
输出
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112
你的原始代码 运行s 是一个谜,因为我使用的 C 编译器都不允许你正在做的事情进行编译,更不用说 运行.
要编译代码,您必须将矩阵更改为指向文字 c 字符串的指针数组,因此
char char_array[4][100]= {"-11111","-1111","-1110","-1112"};
必须
char *char_array[4]= {"-11111","-1111","-1110","-1112"};
您的代码应该是
#include <stdio.h>
#include <stdlib.h>
char *char_array[]= {"-11111","-1111","-1110","-1112"};
#define ARRAY_SIZE sizeof(char_array)/sizeof(char_array[0])
void str_swap(char** a,char** b);
int main() {
size_t j=0;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<ARRAY_SIZE; j++) {
printf("char_array[%zu] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0],&char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<ARRAY_SIZE; j++) {
printf("char_array[%zu] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char** str1, char** str2) {
char* temp;
temp = *str1;
*str1 = *str2;
*str2 = temp;
}
输出将是
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112