;当我使用指向字符串的指针数组时,它显示运行时,或者是因为我无法将数据从指针数组传递给函数
;It shows runtime when i am using an array of pointers to strings,or is it because i cant pass data to a function from array of pointers
#include <stdio.h>
#include <string.h>
//reversal function
void reverseString(char* str)
{
int l, i;
char *begin_ptr, *end_ptr, ch;
l = strlen(str);
begin_ptr = str;
end_ptr = str;
//move the ptr to the final pos
for (i = 0; i < l - 1; i++)
end_ptr++;
//pointer swaping
for (i = 0; i < l / 2; i++)
{
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
begin_ptr++;
end_ptr--;
}
}
// Driver code
--------------------------------主要------------ ---------------------------------------------- --------------------------------------
函数调用发送数组中第一个字符串的地址
int main()
{
char *str[ ] = {"To err is human...","But to really mess things up...","One needs to know C!!"};
for(int i=0;i<3;i++)
{
reverseString(str[i]); //funtion call
printf("Reverse of the string: %s\n", str[i]);
}
return 0;
}
您不能修改字符串文字。任何修改字符串文字的尝试都会导致未定义的行为。
来自 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.
你应该声明一个 to-dimensional 数组,如
enum { N = 32 };
char str[][N] =
{
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
注意函数revreseString太复杂了。当函数 returns 指针指向反转的字符串时也更好。该函数可以使用指针
按以下方式定义
char * reverseString( char *s )
{
if ( *s )
{
for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
{
char c = *p;
*p = *q;
*q = c;
}
}
return s;
}
这是一个演示程序
#include <stdio.h>
#include <string.h>
char * reverseString( char *s )
{
if ( *s )
{
for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
{
char c = *p;
*p = *q;
*q = c;
}
}
return s;
}
int main(void)
{
enum { N = 32 };
char s[][N] =
{
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ); i++ )
{
puts( reverseString( s[i] ) );
}
return 0;
}
程序输出为
...namuh si rre oT
...pu sgniht ssem yllaer ot tuB
!!C wonk ot sdeen enO
#include <stdio.h>
#include <string.h>
//reversal function
void reverseString(char* str)
{
int l, i;
char *begin_ptr, *end_ptr, ch;
l = strlen(str);
begin_ptr = str;
end_ptr = str;
//move the ptr to the final pos
for (i = 0; i < l - 1; i++)
end_ptr++;
//pointer swaping
for (i = 0; i < l / 2; i++)
{
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
begin_ptr++;
end_ptr--;
}
}
// Driver code
--------------------------------主要------------ ---------------------------------------------- -------------------------------------- 函数调用发送数组中第一个字符串的地址
int main()
{
char *str[ ] = {"To err is human...","But to really mess things up...","One needs to know C!!"};
for(int i=0;i<3;i++)
{
reverseString(str[i]); //funtion call
printf("Reverse of the string: %s\n", str[i]);
}
return 0;
}
您不能修改字符串文字。任何修改字符串文字的尝试都会导致未定义的行为。
来自 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
你应该声明一个 to-dimensional 数组,如
enum { N = 32 };
char str[][N] =
{
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
注意函数revreseString太复杂了。当函数 returns 指针指向反转的字符串时也更好。该函数可以使用指针
按以下方式定义char * reverseString( char *s )
{
if ( *s )
{
for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
{
char c = *p;
*p = *q;
*q = c;
}
}
return s;
}
这是一个演示程序
#include <stdio.h>
#include <string.h>
char * reverseString( char *s )
{
if ( *s )
{
for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
{
char c = *p;
*p = *q;
*q = c;
}
}
return s;
}
int main(void)
{
enum { N = 32 };
char s[][N] =
{
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ); i++ )
{
puts( reverseString( s[i] ) );
}
return 0;
}
程序输出为
...namuh si rre oT
...pu sgniht ssem yllaer ot tuB
!!C wonk ot sdeen enO