| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 402 人关注过本帖
标题:二维数组赋值问题
只看楼主 加入收藏
waja
Rank: 2
等 级:论坛游民
帖 子:53
专家分:52
注 册:2012-7-27
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:6 
二维数组赋值问题

#include "stdio.h"
#include  "malloc.h"
struct a
{
   int *p;
   int b;
};

void main()
{
    struct a* temp;
    temp = (struct a*)malloc( sizeof(struct a));
    temp->p = (int *)malloc(2);
    temp->p[0] = 4;//通过
    temp->p[1] = 1;
    printf("%d\n", temp->p[0]);

    struct a* temp1;
    temp1 = (struct a*)malloc( sizeof(struct a));
    temp1->p = (int *)malloc(2*3);
    temp1->p[0][0] = 4;
    printf("%d\n", temp1->p[0][0]);//不通过 怎么给二位数组进行赋值

}
搜索更多相关主题的帖子: include 
2014-03-19 17:12
waja
Rank: 2
等 级:论坛游民
帖 子:53
专家分:52
注 册:2012-7-27
收藏
得分:0 
谁有类似开发的经历呀
2014-03-20 10:02
fragileeye
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:107
专家分:387
注 册:2011-5-21
收藏
得分:0 
对于这样的问题,没必要做过多的解释。只需要记住一点。
编程的时候,不要写自己不懂的代码,这是一个基本的原则!
2014-03-20 10:04
ying8501
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:1092
专家分:1446
注 册:2008-11-24
收藏
得分:10 
#include "stdio.h"
#include  "malloc.h"
struct a
{
   int *p;
   int b;
};

void main()
{
    struct a* temp;
    temp = (struct a*)malloc( sizeof(struct a));
    temp->p = (int *)malloc(2);
    temp->p[0] = 4;//通过
    temp->p[1] = 1;
    printf("%d\n", temp->p[0]);

    struct a* temp1;
    temp1 = (struct a*)malloc( sizeof(struct a));
    temp1->p = (int *)malloc(2*3);
    temp1->p[0][0] = 4; //------这里概念错了。p是指向整型变量的指针,p[0]合理,但[p[0][0]概念错误。
    printf("%d\n", temp1->p[0][0]);//不通过 怎么给二位数组进行赋值

}



[ 本帖最后由 ying8501 于 2014-3-20 10:17 编辑 ]
2014-03-20 10:06
lonely_21
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:108
专家分:395
注 册:2011-11-13
收藏
得分:10 
#include "stdio.h"
#include  "malloc.h"
struct a
{
   int *p;
   int b;
};

void main()
{
    struct a* temp;
    temp = (struct a*)malloc( sizeof(struct a));
    temp->p = (int *)malloc(2); ///这里你是要申请数组么?应该是malloc(sizeof(int)*2)吧
    temp->p[0] = 4;//通过
    temp->p[1] = 1;
    printf("%d\n", temp->p[0]);

    struct a* temp1;
    temp1 = (struct a*)malloc( sizeof(struct a));
    temp1->p = (int *)malloc(2*3); //这里你要写二维数组,同上吧
    temp1->p[0][0] = 4;
    printf("%d\n", temp1->p[0][0]);//不通过 怎么给二位数组进行赋值

}
2014-03-20 10:11
waja
Rank: 2
等 级:论坛游民
帖 子:53
专家分:52
注 册:2012-7-27
收藏
得分:0 
回复 3楼 fragileeye
阅读成功的代码过程中理解作者的思想,从欣赏作者的角度出发,乐此不疲
2014-03-23 19:19
破灬王
Rank: 2
等 级:论坛游民
帖 子:12
专家分:18
注 册:2014-1-17
收藏
得分:0 
顶楼上一个
2014-03-25 21:11
快速回复:二维数组赋值问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015248 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved