[英]C Programming : Parameter : Can't convert to int*
i am new here and i have a question.我是新来的,我有一个问题。
I'm doing my C Programming Assignment about Procedure and Struct.我正在做关于过程和结构的 C 编程作业。
So i want to declare an Array of a Struct, and after that put it as an alias.所以我想声明一个结构数组,然后把它作为别名。 Here is the code :
这是代码:
typedef struct Mahasiswa
{
int NIM;
char NamaMhs[16];
char KodeMK[6];
char Nilai;
}TabMhs[100];
TabMhs M; //Alias
And i want to use this struct as a parameter of another procedure :我想使用这个结构作为另一个过程的参数:
This is the procedure :这是程序:
void SortDataMhs(struct Mahasiswa M[Nmaks],int n);
and this is the procedure call :这是过程调用:
SortDataMhs(&M,n);
But i got an error : [Error] Cannot convert 'Mahasiswa( )][100]' to 'Mahasiswa ' for argument '1' 'void SortDataMhs(Mahasiswa*,int)'但我收到一个错误:[错误] 无法将 'Mahasiswa( )][100]' 转换为 'Mahasiswa ' for argument '1' 'void SortDataMhs(Mahasiswa*,int)'
Any help?有什么帮助吗? And sorry for asking such a newbie question.
很抱歉问这样一个新手问题。 Because i am new to Programming :)
因为我是编程新手:)
This declaration of an array of structure, ie:这个结构数组的声明,即:
typedef struct Mahasiswa
{
int NIM;
char NamaMhs[16];
char KodeMK[6];
char Nilai;
}TabMhs[100];
TabMhs M; //Alias
is not good practice.不是很好的做法。 You can read more about it here .
您可以在此处阅读更多相关信息。
Now, the code below is more readable and understandable:现在,下面的代码更具可读性和可理解性:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Mahasiswa
{
int NIM;
char NamaMhs[16];
char KodeMK[6];
char Nilai;
}TabMhs;
TabMhs m[100];
void ss(TabMhs* m, int n){ // `m` is the pointer to array of structures and `n` is the number of elements in that array.
for(int i=0; i<n ; i++){
printf("%d\t%s\t%s\t%c\t%d\n", m[i].NIM, m[i].NamaMhs, m[i].KodeMK, m[i].Nilai, n);
}
}
int main(){
m[0].NIM = 0;
strcpy(m[0].NamaMhs, "m0nama");
strcpy(m[0].KodeMK, "m0kod");
m[0].Nilai='a';
m[1].NIM = 1;
strcpy(m[1].NamaMhs, "m1nama");
strcpy(m[1].KodeMK, "m1kod");
m[1].Nilai='b';
ss(m,2);
return 0;
}
But say tomorrow your array of structures need more (or less) than 100 elements, in that case, you can make that array dynamic by removing the statement TabMhs m[100] and replacing it with this:但是假设明天您的结构数组需要多于(或少于)100 个元素,在这种情况下,您可以通过删除语句 TabMhs m[100] 并将其替换为以下内容来使该数组动态化:
int main(){
int n = 10;
TabMhs* m = malloc(sizeof(TabMhs) * n);
/* rest of the code remains same */
free(m);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.