- 指针作为形参传递在方法内分配内存并初始化。
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
typedef int T;
typedef struct NODE
{
T data;
struct NODE *next;
} Node;
Node* Init_Node(Node** node);
#endif
Node* Init_Node(Node** node)
{
*node = (Node *)malloc(sizeof(Node));
if(!node)
return NULL;
return node;
}
int main()
{
Node *head;
Init_Node(&head);
if(!head)
return 0;
head->data = 1;
Insert_Node(head,1);
printf("%d**%d", head->data,head->next->data);
return 0;
}
Node* Init_Node(Node* node)
{
node = (Node *)malloc(sizeof(Node));
if(!node)
return NULL;
return node;
}
int main()
{
Node *head;
Init_Node(head);
if(!head)
return 0;
head->data = 1;
Insert_Node(head,1);
printf("%d**%d", head->data,head->next->data);
return 0;
}
- 结构体指针在方法内传递,并修改成员值
int Insert_Node(Node* node,T val)
{
node->data = 3;
printf("%d", head->data);
return 0;
}
- 传递已初始化变量的地址,并在方法内修改值
void swap_num(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x=2,y=5;
swap_num(&x,&y);
printf("x=%d ** y=%d", x,y);
return 0;
}
void swap_num(int *a,int *b)
{
int* temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int x=2,y=5;
swap_num(&x,&y);
printf("x=%d ** y=%d", x,y);
return 0;
}