sizeof(p)/sizeof(*p)
时间: 2023-09-21 13:00:27 浏览: 66
sizeof(p)/sizeof(*p)是一个C语言中常见的用法,用于计算一个数组的元素个数。其中p为指向数组的指针。
sizeof(p)表示指针p所占用的字节大小,sizeof(*p)表示指针p所指向的对象所占用的字节大小。通常情况下,这两个值相等。
通过sizeof(p)/sizeof(*p)的计算,可以得到数组的元素个数。这是因为数组在内存中是连续存储的,而指针p指向数组的首地址。所以,sizeof(p)/sizeof(*p)实际上是计算数组的总字节大小除以每个元素所占用的字节大小,从而得到数组的元素个数。
举个例子,如果有一个数组int arr[5],则sizeof(p)的值为4或8(根据编译器不同而定),sizeof(*p)的值为4。所以,sizeof(p)/sizeof(*p)的结果为5,即数组arr中元素的个数。
需要注意的是,sizeof(p)/sizeof(*p)只适用于静态数组,对于动态数组或者指向动态分配内存的指针,无法使用此方法来计算元素个数。此时需要通过其他方式来记录或者传递数组的长度信息。
相关问题
#include <stdio.h> #include <stdlib.h> #define LEN sizeof(nod) typedef struct node { int data; struct node *next; }nod; nod *create() { /**/ /**/ } int count(nod *head) { /**/ /**/ } int main() { nod *head; head=/**/ /**///调用create函数 printf("%d",/**/ /**/); //调用count函数 return 0; }函数count的功能是:返回链表head中结点的个数。需在第一题写完create函数基础上将count函数与main函数补充完整。
下面是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(nod)
typedef struct node {
int data;
struct node *next;
} nod;
nod *create() {
nod *head, *p1, *p2;
int n, i;
head = NULL;
printf("请输入链表中结点的个数:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
p1 = (nod *) malloc(LEN);
printf("请输入第%d个结点的值:", i);
scanf("%d", &p1->data);
p1->next = NULL;
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
}
return head;
}
int count(nod *head) {
int num = 0;
nod *p = head;
while (p != NULL) {
num++;
p = p->next;
}
return num;
}
int main() {
nod *head;
head = create();
printf("链表中结点的个数为:%d", count(head));
return 0;
}
```
在create函数中,我们使用了一个for循环来创建链表。首先,我们输入了链表中结点的个数n。然后,我们使用一个指针p1来创建新结点,并输入结点的值。如果链表为空,我们将head指向该结点;否则,我们让p2指向最后一个结点,并将p1挂在p2的后面。最后,我们返回head指针,即链表的头指针。
在count函数中,我们使用了一个while循环来遍历链表。我们定义了一个计数器num,并将其初始化为0。然后,我们使用一个指针p来遍历链表,每经过一个结点,我们就将num加1。最后,我们返回num,即链表中结点的个数。
在main函数中,我们首先调用create函数来创建链表,并将返回的头指针赋值给head。然后,我们调用count函数来计算链表中结点的个数,并将结果输出。
#include <stdlib.h> #define LEN sizeof(nod) typedef struct node { int data; struct node *next; }nod; nod *create() { /**/ /**/ } void print(nod *head) { /**/ /**/ } int main() { nod *head; head=/**/ /**/ //调用create函数 /**/ /**/ //调用print函数 return 0; }函数create()的功能是:逆向建立动态链表,返回链表头指针head,要求从键盘倒序输入各结点的数据,当输入值为0时停止结点的建立。print函数输出链表head中各个结点的data值。请将create函数和print函数补充完整。
函数create()的代码实现如下:
```c
nod *create() {
nod *head=NULL, *p;
int data;
scanf("%d",&data);
while(data!=0) {
p=(nod*)malloc(LEN);
p->data=data;
p->next=head;
head=p;
scanf("%d",&data);
}
return head;
}
```
函数print()的代码实现如下:
```c
void print(nod *head) {
nod *p=head;
while(p!=NULL) {
printf("%d ",p->data);
p=p->next;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)