下面是我为类项目编写的代码。我遇到了一些我无法弄清楚的问题。第一个问题是,在以下几行中我不断收到冲突类型的错误。
int sortsize(struct sampleInfo);
int newSortsize = sortsize(struct sampleInfo);
和
int sortid(struct sampleInfo);
int newSortid = sortid(struct sampleInfo);
我似乎无法弄清楚出了什么问题。我遇到的第二个问题源于第一个问题,因为我无法调用这些函数来打印出我需要的结构数组。
#include <stdio.h>
#include <time.h>
struct sampleInfo
{
char sample;
int id;
int size;
double value;
} sample[15];
struct sampleInfo id() //int
{
sample[0].id = rand() % 100 + 1;
sample[1].id = rand() % 100 + 1;
sample[2].id = rand() % 100 + 1;
sample[3].id = rand() % 100 + 1;
sample[4].id = rand() % 100 + 1;
sample[5].id = rand() % 100 + 1;
sample[6].id = rand() % 100 + 1;
sample[7].id = rand() % 100 + 1;
sample[8].id = rand() % 100 + 1;
sample[9].id = rand() % 100 + 1;
sample[10].id = rand() % 100 + 1;
sample[11].id = rand() % 100 + 1;
sample[12].id = rand() % 100 + 1;
sample[13].id = rand() % 100 + 1;
sample[14].id = rand() % 100 + 1;
}
struct sampleInfo size() //int
{
sample[0].size = 1;
sample[1].size = 2;
sample[2].size = 3;
sample[3].size = 4;
sample[4].size = 5;
sample[5].size = 6;
sample[6].size = 7;
sample[7].size = 8;
sample[8].size = 9;
sample[9].size = 10;
sample[10].size = 11;
sample[11].size = 12;
sample[12].size = 13;
sample[13].size = 14;
sample[14].size = 15;
}
struct sampleInfo value() //double
{
sample[0].value = 1.000000;
sample[1].value = 2.000000;
sample[2].value = 3.000000;
sample[3].value = 4.000000;
sample[4].value = 5.000000;
sample[5].value = 6.000000;
sample[6].value = 7.000000;
sample[7].value = 8.000000;
sample[8].value = 9.000000;
sample[9].value = 10.000000;
sample[10].value = 11.000000;
sample[11].value = 12.000000;
sample[12].value = 13.000000;
sample[13].value = 14.000000;
sample[14].value = 15.000000;
}
struct sampleInfo stringvalue() //char
{
strcpy(sample[0].sample, "one");
strcpy(sample[1].sample, "two");
strcpy(sample[2].sample, "three");
strcpy(sample[3].sample, "four");
strcpy(sample[4].sample, "five");
strcpy(sample[5].sample, "six");
strcpy(sample[6].sample, "seven");
strcpy(sample[7].sample, "eight");
strcpy(sample[8].sample, "nine");
strcpy(sample[9].sample, "ten");
strcpy(sample[10].sample, "eleven");
strcpy(sample[11].sample, "twelve");
strcpy(sample[12].sample, "thirteen");
strcpy(sample[13].sample, "fourteen");
strcpy(sample[14].sample, "fifteen");
}
char userinput()
{
char choice[4];
printf("Do you want to sort the samples by id or size?\n");
scanf("%s",&choice);
printf("Your choice is %s\n",choice);
return choice;
}
int sortsize(struct sampleInfo sample[15])
{
int i;
int maxsize;
maxsize = 0;
for (i = 0; i < 14; i++)
{
if (sample[i].size >= maxsize)
{
maxsize = sample[i].size;
sample[i++].size;
}
else;
}
printf("You array sorted by id is %d",&sample[15]);
}
int sortid(struct sampleInfo sample[15])
{
printf("test");
int i;
int maxid;
maxid = 0;
for (i = 0; i < 14; i++)
{
if (sample[i].id >= maxid)
{
maxid = sample[i].id;
sample[i++].id;
}
else;
}
printf("You array sorted by id is %d",&sample[15]);
}
int main (char choice)
{
time_t seconds = time(NULL);
int seed = (unsigned)(seconds);
srand(seed);
char userinput();
char response = userinput();
if (choice == "id")
{
int sortsize(struct sampleInfo);
int newSortsize = sortsize(struct sampleInfo);
}
else
{
int sortid(struct sampleInfo);
int newSortid = sortid(struct sampleInfo);
}
}
答案 0 :(得分:5)
您的sortsize
和sortid
函数有多个相互矛盾的声明。您将 sortsize
定义为:
int sortsize(struct sampleInfo sample[15]) { ... }
这意味着函数需要一个参数,它是struct sampleInfo
的数组(从技术上讲,它实际上需要一个指针,但我们不会在这里进入)。函数 definition 也可用作函数声明,至少在同一个翻译单元(源文件)中。
然后,在if
语句的正文中,您将函数的新声明写为
int sortsize(struct sampleInfo);
第二个声明与之前的定义不匹配 - 在第一种情况下,函数期望接收指向struct sampleInfo
的指针,而在第二种情况下期望单个值struct sampleInfo
的值。您对编译器感到困惑。
由于您之前已经定义了该函数的声明,因此您可以完全删除该第二个声明。
但是,现在您遇到了一个新问题 - 您的函数调用被写为
int newSortsize = sortsize(struct sampleInfo);
在函数调用中,您不必再次指定参数的类型;传递所需类型的对象。由于您的函数定义需要struct sampleInfo
的数组,因此您需要传递相同类型的对象,在本例中为sample
阵列:
int newSortSize = sortsize( sample );
以下是代码的读取方式:
if ( strcmp( choice, "id" ) == 0 ) // can't compare strings using == operator
{
int newSortsize = sortsize( sample );
}
else
{
int newSortid = sortid( sample );
}
有一点需要注意,正如所写的那样,这并不是非常有用。但是,它应该让你超越那个特定的错误。
您的代码中还有其他致命问题 - userinput
会触发至少一个编译错误(并且无论如何都不会做你想做的事),sortid
和sortsize
don& #39;实际上返回任何内容,并且您的格式设置为一团糟。您需要退后一步,找到一个好的C引用,并重新思考您正在做什么。
修改强>
不是逐行检查代码并指出错误,而是重新编写它更快。我不会为这样做感到非常内疚,因为你已经真诚地努力编写自己的代码。这可能不是我最高质量的工作,但至少应该像你期望的那样建立和运行或多或少。
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <stdlib.h>
#define MAX_SAMPLE_LENGTH 10 // Longest sample string length, not counting
// string terminator
/**
* Define the sampleInfo type; however, do not create an instance
* at file scope. Instead, we'll create an instance in the main
* function and pass that instance as an argument to all the other
* functions.
*/
struct sampleInfo
{
char sample[MAX_SAMPLE_LENGTH + 1]; // Need an array of `char` to store string values
int id;
int size;
double value;
};
/**
* change `id` to take an array of struct sampleInfo as input,
* rather than rely on a global variable. Also, since the function
* doesn't actually *return* anything, change the return type to void
*/
void id( struct sampleInfo *sample, size_t count )
{
/**
* Use a loop to iterate through the array. Using the % operator
* isn't the best way to map the result of rand() to a smaller
* range, but it's good enough for now.
*/
for ( size_t i = 0; i < count; i++ )
sample[i].id = rand() % 100 + 1;
}
/**
* Same changes as above
*/
void size( struct sampleInfo *sample, size_t count) //int
{
for ( size_t i = 0; i < count; i++ )
sample[i].size = i + 1;
}
/**
* Same changes as above
*/
void value( struct sampleInfo *sample, size_t count )
{
for ( size_t i = 0; i < count; i++ )
sample[i].value = i + 1.0;
}
/**
* This one doesn't work as a loop, but we add a check to make
* sure our input array is large enough to accept all the
* strings, otherwise we print an error and exit.
*/
void stringvalue(struct sampleInfo *sample, size_t count )
{
if ( count < 15 )
{
fprintf( stderr, "SAMPLE ARRAY IS TOO SMALL\n" );
exit( 0 );
}
strcpy(sample[0].sample, "one");
strcpy(sample[1].sample, "two");
strcpy(sample[2].sample, "three");
strcpy(sample[3].sample, "four");
strcpy(sample[4].sample, "five");
strcpy(sample[5].sample, "six");
strcpy(sample[6].sample, "seven");
strcpy(sample[7].sample, "eight");
strcpy(sample[8].sample, "nine");
strcpy(sample[9].sample, "ten");
strcpy(sample[10].sample, "eleven");
strcpy(sample[11].sample, "twelve");
strcpy(sample[12].sample, "thirteen");
strcpy(sample[13].sample, "fourteen");
strcpy(sample[14].sample, "fifteen");
}
/**
* Instead of returning a local array (which doesn't work), we
* pass our input buffer as an argument to the function.
*/
int userinput( char *choice, size_t bufsize )
{
printf("Do you want to sort the samples by id or size?\n");
if ( fgets( choice, bufsize, stdin ) )
{
// clear out the newline character, if present
char *newline = strchr( choice, '\n' );
if ( newline )
*newline = 0;
printf("Your choice is %s\n",choice);
return 1;
}
return 0;
}
/**
* Find *and return* the largest size value.
*/
int sortsize(struct sampleInfo *sample, size_t count )
{
int i;
int maxsize = INT_MIN; // taken from limits.h
for (i = 0; i < count; i++)
{
if (sample[i].size >= maxsize)
{
maxsize = sample[i].size; // there is no need to manually update i;
} // that's handled by the loop control
} // expression.
return maxsize;
}
/**
* Same sort of changes as above.
*/
int sortid(struct sampleInfo *sample, size_t count )
{
printf("test");
int i;
int maxid = 0;
for (i = 0; i < count; i++)
{
if (sample[i].id >= maxid)
{
maxid = sample[i].id;
}
}
return maxid;
}
/**
* The main function has two forms:
*
* int main( void )
* int main( int argc, char **argv )
*
* Use the second form when you pass command line arguments to the program.
* argc indicates the number of command line arguments (including the command
* used to start the program) and argv is an array of the argument
* strings.
*
* In this case, we're not using command-line arguments, so we use the first
* form.
*/
int main ( void )
{
/**
* Create our sample array within the main function and pass it as
* an argument to the other functions.
*/
struct sampleInfo sample[15];
/**
* When we pass an array as an argument to a function it loses its
* "array-ness", and what the function receives is just a pointer
* to the first element. There's no way for the function to know
* how many elements are in the array from that pointer value alone,
* so we have to pass that count as a separate argument. The following
* is a common trick to determine how many elements are in an array
* object - we divide the size of the array (in bytes) by the size
* of a single element of the array (also in bytes).
*/
size_t sampleCount = sizeof sampleInfo / sizeof sampleInfo[0];
srand( time( NULL ) );
/**
* Initialize the contents of the sample array. Ideally, these four
* functions could be consolidated into a single function, but we'll
* leave it as it is for now.
*/
id( sample, sampleCount );
size( sample, sampleCount );
value( sample, sampleCount );
stringValue( sample, sampleCount );
/**
* Create a buffer to store our user input; the buffer size needs to
* be at least one element longer than our longest expected input. If
* "size" is your longest expected input, then the buffer needs to
* be *at least* 5 characters wide. I'm making it 6 to account for the
* newline character as well.
*/
char response[6];
if ( !userInput( response, sizeof response ) )
{
fprintf( "Error on input...exiting\n" );
exit( EXIT_FAILURE );
}
/**
* You can't use the == operator to compare strings (or any array
* expressions). You need to use strcmp here:
*/
if ( strcmp( choice, "id" ) == 0)
{
int newSortsize = sortsize( sample, sampleCount );
/*
* do something useful with newSortSize
*/
}
else
{
int newSortid = sortid( sample, sampleCount );
/*
* do something useful with newSortId
*/
}
/**
* As of C99, main doesn't have to actually return a value,
* but I think it's a good idea to do so anyway.
*/
return EXIT_SUCCESS;
}