我写了一个顺序表的简单操作, 一直不理解这个指针的用法。

逍安博 2020-09-03 10:35:48
#include <stdio.h>
#include <stdlib.h>

#define LIST_MAX 10

typedef struct {
int List_data[LIST_MAX];
int Lenth;
}SqrList;

void List_insert(SqrList &L,int NUM, int Change)
{
for(int j=L.Lenth;j>=NUM;j-- )
{
L.List_data[j] = L.List_data[j-1];
}
L.List_data[NUM-1] = Change;
L.Lenth++;
}

void List_Init(SqrList &L )
{

L.Lenth = 6 ;
for(int i = 0; i<L.Lenth;i++)
{
L.List_data[i] = 0;
}
}

void List_printf(SqrList &L)
{
for(int i=0;i<L.Lenth;L++)
{
printf("顺序表的第%d个的值:%d",i,L.List_data[i]);
}

}

int main()
{
SqrList L;

List_Init(L);

List_printf(L);

List_insert(L,5,4);

List_printf(L);

return 0;
}



error: expected ';', ',' or ')' before '&' token|
error: expected ';', ',' or ')' before '&' token|

为什么会报这个错误呀?

...全文
102 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chxchxkkk 2020-09-04
  • 打赏
  • 举报
回复
C语言不支持&传参吧
自信男孩 2020-09-04
  • 打赏
  • 举报
回复
引用 7 楼 自信男孩 的回复:
#include <stdio.h>
#include <stdlib.h>

#define LIST_MAX 10

typedef struct {
int List_data[LIST_MAX];
int Lenth;
}SqrList;

void List_insert(SqrList *L,int NUM, int Change)
{
for(int j=L->Lenth;j>=NUM;j-- )
{
L->List_data[j] = L->List_data[j-1];
}
L->List_data[NUM-1] = Change;
L->Lenth++;
}

void List_Init(SqrList *L )
{

L->Lenth = 6 ;
for(int i = 0; i<L->Lenth;i++)
{
L->List_data[i] = 0;
}
}

//void List_printf(SqrList &L)
void List_printf(SqrList *L)
{
//for(int i=0;i<L.Lenth;L++)
for(int i=0;i<L->Lenth;i++)
{
printf("顺序表的第%d个的值:%d\n",i,L->List_data[i]);
}

}

int main()
{
SqrList L;

List_Init(&L);

List_printf(&L);

List_insert(&L,5,4);

List_printf(&L);

return 0;
}

供参考~

用指针替换应用~

不是应用而是引用&
自信男孩 2020-09-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

#define LIST_MAX 10

typedef struct {
int List_data[LIST_MAX];
int Lenth;
}SqrList;

void List_insert(SqrList *L,int NUM, int Change)
{
for(int j=L->Lenth;j>=NUM;j-- )
{
L->List_data[j] = L->List_data[j-1];
}
L->List_data[NUM-1] = Change;
L->Lenth++;
}

void List_Init(SqrList *L )
{

L->Lenth = 6 ;
for(int i = 0; i<L->Lenth;i++)
{
L->List_data[i] = 0;
}
}

//void List_printf(SqrList &L)
void List_printf(SqrList *L)
{
//for(int i=0;i<L.Lenth;L++)
for(int i=0;i<L->Lenth;i++)
{
printf("顺序表的第%d个的值:%d\n",i,L->List_data[i]);
}

}

int main()
{
SqrList L;

List_Init(&L);

List_printf(&L);

List_insert(&L,5,4);

List_printf(&L);

return 0;
}

供参考~

用指针替换应用~
qybao 2020-09-04
  • 打赏
  • 举报
回复
引用 5 楼 克制、学 的回复:
[quote=引用 1 楼 chxchxkkk 的回复:]C语言不支持&传参吧
但是我后面改了,回报这个错误 request for member 'List_data' in something not a structure or union|[/quote]你改成指针的形式就不能用点.操作符了L.List_data,要改成->操作符L->List_data,或者改成(*L).List_data
逍安博 2020-09-04
  • 打赏
  • 举报
回复
引用 1 楼 chxchxkkk 的回复:
C语言不支持&传参吧
但是我后面改了,回报这个错误 request for member 'List_data' in something not a structure or union|
逍安博 2020-09-04
  • 打赏
  • 举报
回复
引用 2 楼 qybao 的回复:
List_printf函数的for(int i=0;i<L.Length;L++)的L++改成i++。
多谢,这是我的一个错误。
逍安博 2020-09-04
  • 打赏
  • 举报
回复
引用 1 楼 chxchxkkk 的回复:
C语言不支持&传参吧
我有一点印象, 这个好像是C++的; 指针真的是有点晕乎;
qybao 2020-09-04
  • 打赏
  • 举报
回复
List_printf函数的for(int i=0;i<L.Length;L++)的L++改成i++。
逍安博 2020-09-04
  • 打赏
  • 举报
回复
引用 8 楼 自信男孩 的回复:
[quote=引用 7 楼 自信男孩 的回复:]
#include <stdio.h>
#include <stdlib.h>

#define LIST_MAX 10

typedef struct {
    int List_data[LIST_MAX];
    int Lenth;
}SqrList;

void List_insert(SqrList *L,int NUM, int Change)
{
    for(int j=L->Lenth;j>=NUM;j-- )
    {
        L->List_data[j] = L->List_data[j-1];
    }
    L->List_data[NUM-1] = Change;
    L->Lenth++;
}

void List_Init(SqrList *L )
{

    L->Lenth = 6 ;
    for(int i = 0; i<L->Lenth;i++)
    {
        L->List_data[i] = 0;
    }
}

//void List_printf(SqrList &L)
void List_printf(SqrList *L)
{
    //for(int i=0;i<L.Lenth;L++)
    for(int i=0;i<L->Lenth;i++)
    {
        printf("顺序表的第%d个的值:%d\n",i,L->List_data[i]);
    }

}

int main()
{
    SqrList L;

    List_Init(&L);

    List_printf(&L);

    List_insert(&L,5,4);

    List_printf(&L);

    return 0;
}
供参考~ 用指针替换应用~
不是应用而是引用&[/quote] 正确了, 这个指针的确灵活。
逍安博 2020-09-04
  • 打赏
  • 举报
回复
引用 6 楼 qybao 的回复:
[quote=引用 5 楼 克制、学 的回复:][quote=引用 1 楼 chxchxkkk 的回复:]C语言不支持&传参吧
但是我后面改了,回报这个错误 request for member 'List_data' in something not a structure or union|[/quote]你改成指针的形式就不能用点.操作符了L.List_data,要改成->操作符L->List_data,或者改成(*L).List_data[/quote] 是的, 正确了。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧