(p+i)->no是什么意思
时间: 2024-03-29 17:35:48 浏览: 15
`(p+i)->no`是指针p所指向的数组中第i个元素的no成员,其中箭头符号->是一个指向结构体成员的运算符。因为p是一个指向STUDENT类型的指针,所以`(p+i)`表示指针p偏移i个STUDENT类型的大小,指向数组中第i个元素。然后箭头符号->用于访问该元素的no成员,相当于`(*(p+i)).no`。
相关问题
#define _CRT_SECURE_NO_WARNINGS #include"stdio.h" struct shuju { char p_name[100], c_name[100], zhiwu[100],num[100]; int age; }; void input(struct shuju p, int m) { int i; for (i = 1; i <= m; i++,p++) { printf("请输入第%d个客户的信息:\n", i); printf("请输入客户姓名:"); scanf("%s", &p->p_name); printf("请输入客户年龄:"); scanf("%d", &p->age); printf("请输入客户手机号:"); scanf("%s", &p->num); printf("请输入客户公司名:"); scanf("%s", &p->c_name); printf("请输入客户职务:"); scanf("%s", &p->zhiwu); } } void P(char q, int m) { struct shuju* p; struct shuju*a; q = a->p_name; int i,j; for (i = 0; i < m; i++) { for (j = 0; j < 100; j++) { if ((q + j) < *(q + 1 + j)) { *p = *(a + i); *(a + i) = *(a + 1 + i); *(a + 1 + i) = p; } } } } void output(struct shuju p, int m) { int i; for (i = 0; i < m; i++) { printf("姓名\t年龄\t手机号\t公司名\t职务\n"); printf("%s\t%d\t%s\t%s\t%s\n", (p + i)->p_name, (p + i)->age, (p + i)->num, (p + i)->c_name,(p + i)->zhiwu); } } void main() { int n; struct shuju c; char q; q = c->p_name; input(c, n); P(q, n); output(c, n); }这段代码怎么修改才正确
这段代码需要修改的地方有很多,以下是其中几个需要修改的地方:
1. 在函数中传递结构体时,应该传递结构体指针,而不是结构体本身。因此,函数 input 和 output 中应该改为传递 struct shuju* 类型的指针参数。
2. 在函数 P 中,a 没有被初始化,不能直接用指针 a 访问结构体成员。可以在 main 函数中定义一个 struct shuju 类型的数组,并把它的地址传递给 P 函数。
3. 在函数 P 中,比较字符串应该使用 strcmp 函数,而不是直接比较字符指针。
4. 在函数 P 中,交换结构体时,应该使用结构体指针进行交换,而不是结构体本身。
5. 在 main 函数中,应该先读入 n 的值,再定义 struct shuju 类型的数组 c。
下面是修改后的代码:
```
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "string.h"
struct shuju {
char p_name[100], c_name[100], zhiwu[100], num[100];
int age;
};
void input(struct shuju* p, int m) {
int i;
for (i = 0; i < m; i++, p++) {
printf("请输入第%d个客户的信息:\n", i+1);
printf("请输入客户姓名:");
scanf("%s", p->p_name);
printf("请输入客户年龄:");
scanf("%d", &p->age);
printf("请输入客户手机号:");
scanf("%s", p->num);
printf("请输入客户公司名:");
scanf("%s", p->c_name);
printf("请输入客户职务:");
scanf("%s", p->zhiwu);
}
}
void P(struct shuju* a, int m) {
struct shuju* p;
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < m - i - 1; j++) {
if (strcmp(a[j].p_name, a[j + 1].p_name) > 0) {
p = &a[j];
a[j] = a[j + 1];
a[j + 1] = *p;
}
}
}
}
void output(struct shuju* p, int m) {
int i;
printf("姓名\t年龄\t手机号\t公司名\t职务\n");
for (i = 0; i < m; i++) {
printf("%s\t%d\t%s\t%s\t%s\n", p[i].p_name, p[i].age, p[i].num, p[i].c_name, p[i].zhiwu);
}
}
int main() {
int n;
struct shuju c[100];
printf("请输入客户个数:");
scanf("%d", &n);
input(c, n);
P(c, n);
output(c, n);
return 0;
}
```
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #define MAXV 1000 #define ElemType int #define INF 32767typedef struct { int no; int info; }VertexType; typedef struct{ int edges[MAXV][MAXV]; int n,e; VertexType vexs[MAXV]; }MatGraph; typedef struct ArcNode{ int adjvex; int weight; struct ArcNode *nextarc; }ArcNode; typedef struct VNode{ VertexType data; ArcNode *firstarc; }VNode,AdjList[MAXV]; typedef struct{ AdjList adjlist; int n,e; }AdjGraph; void CreateAdj(AdjGraph *&G,int A [MAXV][MAXV],int n,int e){ int i,j;ArcNode *p; G=(AdjGraph *)malloc(sizeof(AdjGraph)); for(i=0;i<n;i++) { G->adjlist[i].firstarc=NULL; } for(i=0;i<n;i++) { for(j=n-1;j>=0;j--) { if(A[i][j]!=0 && A[i][j]!=INF) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->weight=A[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } } } G->n=n;G->e=e; }void DispAdj(AdjGraph *G) { int i;ArcNode *p; for(i=0;i<G->n;i++) { p=G->adjlist[i].firstarc; printf("%3d:",i); while(p!=NULL) { printf("%3d[%d]->",p->adjvex,p->weight); p=p->nextarc; } printf("^\n"); } }typedef struct{ int data[MAXV]; int front,rear; }SqQueue; void InitQueue(SqQueue *&q){ q=(SqQueue *)malloc(sizeof(SqQueue)); q->front=q->rear=-1; } void DestroyQueue(SqQueue *&q){ free(q); } bool QueueEmpty(SqQueue *q){ return q->front == q->rear; } bool enQueue(SqQueue *&q,int e){ if(q->rear ==MAXV -1){ return false; } q->rear++; q->data[q->rear]=e; return true; } bool deQueue(SqQueue *&q,int &e){ if(q->front ==q->rear){ return false; } q->front++; e=q->data[q->front]; return true; }MatGraph *CreateMat(char a[],int n,int e) { MatGraph *G=(MatGraph *)malloc(sizeof(MatGraph)); int i,j,k; G->n=n; G->e=e; for(i=0;i<n;i++) { G->vexs[i].no=i; G->vexs[i].info=a[i]; } for(i=0;i<n;i++) { for(j=0;j<n;i++) { G->edges[i][j]=0; } } for(k=0;k<e;k++) { printf("输入相邻的顶点:"); scanf("%d",&i); G->edges[i][j]=1; G->edges[j][i]=1; } return G; } int main(){ int n=7,e=12; char a[]={'0','1','2','3','4','5','6'}; MatGraph *G=CreateMat(a,n,e); AdjGraph *H; CreateAdj(H,G->edges,n,e); DFS(G,v); return 0; }修改上述代码
在上述代码中需要修改的地方有:
1. 在CreateMat函数中,第二个for循环的条件应该是j<n而不是i<n。
2. 在CreateMat函数中,输入相邻顶点时应该输入j而不是i。
3. 在main函数中,DFS函数的参数应该是G而不是v。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define MAXV 1000
#define ElemType int
#define INF 32767
typedef struct {
int no;
int info;
} VertexType;
typedef struct{
int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
} MatGraph;
typedef struct ArcNode{
int adjvex;
int weight;
struct ArcNode *nextarc;
} ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
} VNode,AdjList[MAXV];
typedef struct{
AdjList adjlist;
int n,e;
} AdjGraph;
void CreateAdj(AdjGraph *&G,int A [MAXV][MAXV],int n,int e){
int i,j;
ArcNode *p;
G=(AdjGraph *)malloc(sizeof(AdjGraph));
for(i=0;i<n;i++)
{
G->adjlist[i].firstarc=NULL;
}
for(i=0;i<n;i++)
{
for(j=n-1;j>=0;j--)
{
if(A[i][j]!=0 && A[i][j]!=INF)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->weight=A[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
}
}
G->n=n;
G->e=e;
}
void DispAdj(AdjGraph *G) {
int i;
ArcNode *p;
for(i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
printf("%3d:",i);
while(p!=NULL)
{
printf("%3d[%d]->",p->adjvex,p->weight);
p=p->nextarc;
}
printf("^\n");
}
}
typedef struct{
int data[MAXV];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q){
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=-1;
}
void DestroyQueue(SqQueue *&q){
free(q);
}
bool QueueEmpty(SqQueue *q){
return q->front == q->rear;
}
bool enQueue(SqQueue *&q,int e){
if(q->rear ==MAXV -1){
return false;
}
q->rear++;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,int &e){
if(q->front ==q->rear){
return false;
}
q->front++;
e=q->data[q->front];
return true;
}
void DFS(MatGraph *G,int v){
int i;
printf("%d ",v);
visited[v]=1;
for(i=0;i<G->n;i++){
if(G->edges[v][i]!=0 && !visited[i]){
DFS(G,i);
}
}
}
MatGraph *CreateMat(char a[],int n,int e) {
MatGraph *G=(MatGraph *)malloc(sizeof(MatGraph));
int i,j,k;
G->n=n;
G->e=e;
for(i=0;i<n;i++)
{
G->vexs[i].no=i;
G->vexs[i].info=a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
G->edges[i][j]=0;
}
}
for(k=0;k<e;k++)
{
int i,j;
printf("输入相邻的顶点:");
scanf("%d %d",&i,&j);
G->edges[i][j]=1;
G->edges[j][i]=1;
}
return G;
}
int main(){
int n=7,e=12;
char a[]={'0','1','2','3','4','5','6'};
MatGraph *G=CreateMat(a,n,e);
AdjGraph *H;
CreateAdj(H,G->edges,n,e);
DFS(G,0);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)