我是 C 语言初学者,正在学习 C 语言中的函数指针。还有我遇到的问题?
编写一个比较函数来按名称的第一个字符排序?
* int ( firstnamecharcompar)(const void * a, const void * b))
这是我的代码解决方案。
#include<stdlib.h>
#include<stdio.h>
#include <stdbool.h>
int compare1(const void *a,const void *b)
{
char *c = *(char**)a;
char *d = *(char**)b;
return c[0] - d[0];
//return ( *(char*)a[0] == *(char*)b[0] );
}
int main()
{
char str[3][10];
int i;
for(i=0;i<3;i++)
{
printf("Enter %d string => " , i+1 );
scanf("%s", str[i]);
printf("\n");
}
for(i=0;i<3;i++)
{
printf("%s ",str[i]);
}
qsort(str,3,10,compare1);
for(i=0;i<3;i++)
{
printf("%s ",str[i]);
} …Run Code Online (Sandbox Code Playgroud) 我正在学习 C++ 中的继承。因此,每当创建类的对象时,都会调用构造函数。构造函数用于初始化类变量。
#include<bits/stdc++.h>
using namespace std;
class Base
{
protected:
int x;
public:
Base(int a): x(a)
{
cout<<"Base"<<endl;
}
};
class Derived: public Base
{
private:
int y;
public:
Derived(int b):y(b)
{
cout<<"Derived"<<endl;
}
void print()
{
cout<<x<<" "<<y<<endl;
}
};
int main()
{
Derived d(20);
d.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从这里开始,我正在创建基类的对象并在其上调用打印函数。所以我应该得到输出。但是我的代码给出了编译器错误,为什么?任何人都可以帮助我理解这一点吗?
我是 C 语言初学者,正在学习 C 语言中的函数指针。还有我遇到的问题?
编写一个比较函数来按名称的第一个字符排序?
*int (firstnamecharcompar)(const void * a, const void * b))
这是我的代码解决方案。
#include<stdlib.h>
#include<stdio.h>
#include <stdbool.h>
int compare1(const void *a,const void *b)
{
char *c = *(char**)a;
char *d = *(char**)b;
return c[0] - d[0];
//return ( *(char*)a[0] == *(char*)b[0] );
}
int main()
{
char* str[3];
int i;
for(i=0;i<3;i++)
{
str[i] = (char*)malloc(10*sizeof(char));
}
for(i=0;i<3;i++)
{
printf("Enter %d string => " , i+1 );
scanf("%s", str[i]);
printf("\n");
}
for(i=0;i<3;i++)
{
printf("%s ",str[i]);
}
qsort(str,3,10,compare1);
for(i=0;i<3;i++) …Run Code Online (Sandbox Code Playgroud)