linux中C语言程序疏失
linux中C语言程序出错
程序如下,错误提示程序中注明了。改程序是在linux系统中,由g++编译时候的错误提示。请教是何原因
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20
#define MAX_INPUT 1000
int read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input, int n_columns, int const columns[] );
错误提示:expect initiater before "void"
int main( void )
{
int n_columns;
int columns[MAX_COLS];
char input[MAX_INPUT];
char output[MAX_INPUT];
错误提示:主函数中没有 read_column_numbers... 和 rearrange... 两个函数的声明。
这个程序是《C和指针》中的函数,在linux系统中运行需要作调整吗?
谢谢!
n_columns = read_column_numbers( columns, MAX_COLS );
while( gets( input ) != NULL )
{
printf( “Original input : %s\n”, input );
rearrange( output, input, n_columns, columns );
printf( “Rearranged line: %s\n”, output );
}
return EXIT_SUCCESS;
}
int read_column_numbers( int columns[], int max )
{
int num = 0;
int ch;
while( num < max && scanf( “%d”, &columns[num] ) == 1 && columns[num] >= 0 )
num +=1;
if( num % 2 != 0 )
{
puts( “Last column number is not paired.”);
exit( EXIT_FAILURE );
}
while( (ch = getchar() ) != EOF && ch!= ‘\n’ )
;
return num;
}
void rearrange( char *output, char const *input, int n_columns, int const columns[] )
{
int col;
int output_col;
int len;
len = strlen( input );
output_col = 0;
for ( col = 0; col < n_columns; col += 2)
{
int nchars = columns[col +1] – columns[col] +1;
if ( columns[col] >= len || output_col == MAX_INPUT -1 )
break;
if( output_col + nchars > MAX_INPUT -1)
nchars = MAX_INPUT – output_col -1;
strcpy( output + output_col, input + coulunms[col], nchars );
output_col += nchars;
}
output[output_col] = ‘\0’ ;
}
------解决方案--------------------
倒数第5行改成strncpy(output + output_col, input + columns[col], nchars),另外最好不要用gets,改用fgets或者是scanf比较安全。
------解决方案--------------------
错误提示:expect initiater before "void"
半角字符?
------解决方案--------------------
------解决方案--------------------
既然如此的话,那么还得麻烦贴上正确的代码,否则如何帮你分析。
即使你拷贝到word中,也不至于把strncpy写成strcpy,columns写成coulunms啊,而且一堆的全角字符。
------解决方案--------------------
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
------解决方案--------------------
我也在Linux下是可以编译通过的啊,只是有警告信息而已!!你是用gcc命令的吧
------解决方案--------------------
这样不是需要定位很久?
程序如下,错误提示程序中注明了。改程序是在linux系统中,由g++编译时候的错误提示。请教是何原因
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20
#define MAX_INPUT 1000
int read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input, int n_columns, int const columns[] );
错误提示:expect initiater before "void"
int main( void )
{
int n_columns;
int columns[MAX_COLS];
char input[MAX_INPUT];
char output[MAX_INPUT];
错误提示:主函数中没有 read_column_numbers... 和 rearrange... 两个函数的声明。
这个程序是《C和指针》中的函数,在linux系统中运行需要作调整吗?
谢谢!
n_columns = read_column_numbers( columns, MAX_COLS );
while( gets( input ) != NULL )
{
printf( “Original input : %s\n”, input );
rearrange( output, input, n_columns, columns );
printf( “Rearranged line: %s\n”, output );
}
return EXIT_SUCCESS;
}
int read_column_numbers( int columns[], int max )
{
int num = 0;
int ch;
while( num < max && scanf( “%d”, &columns[num] ) == 1 && columns[num] >= 0 )
num +=1;
if( num % 2 != 0 )
{
puts( “Last column number is not paired.”);
exit( EXIT_FAILURE );
}
while( (ch = getchar() ) != EOF && ch!= ‘\n’ )
;
return num;
}
void rearrange( char *output, char const *input, int n_columns, int const columns[] )
{
int col;
int output_col;
int len;
len = strlen( input );
output_col = 0;
for ( col = 0; col < n_columns; col += 2)
{
int nchars = columns[col +1] – columns[col] +1;
if ( columns[col] >= len || output_col == MAX_INPUT -1 )
break;
if( output_col + nchars > MAX_INPUT -1)
nchars = MAX_INPUT – output_col -1;
strcpy( output + output_col, input + coulunms[col], nchars );
output_col += nchars;
}
output[output_col] = ‘\0’ ;
}
------解决方案--------------------
倒数第5行改成strncpy(output + output_col, input + columns[col], nchars),另外最好不要用gets,改用fgets或者是scanf比较安全。
------解决方案--------------------
错误提示:expect initiater before "void"
半角字符?
------解决方案--------------------
------解决方案--------------------
既然如此的话,那么还得麻烦贴上正确的代码,否则如何帮你分析。
即使你拷贝到word中,也不至于把strncpy写成strcpy,columns写成coulunms啊,而且一堆的全角字符。
------解决方案--------------------
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
------解决方案--------------------
我也在Linux下是可以编译通过的啊,只是有警告信息而已!!你是用gcc命令的吧
------解决方案--------------------
这样不是需要定位很久?