View Full Version : اشاره گری به شی یک struct
mrsh_1988
سه شنبه 07 دی 1389, 12:38 عصر
سلام
سوال: struct به نام stud که شامل نام و ... باشه . شی از این struct بسازیم . اشاگری به این شی بسازیم و با این اشاره گر به عناصر struct مقدار دهی کنیم.
مثلا s1 را از struct بسازیم. و شی اشاره گر ps1 به s1 باشد . با استفاده از ps1 اعضای داده ای struct را مقدار دهی کنیم.
Arcsinos
سه شنبه 07 دی 1389, 13:33 عصر
منظورتو دقیق نفهمیدم ولی فکر میکنم این برنامه باشه :
// struct.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "string.h"
using namespace std;
struct stud
{
int id;
};
struct stud2
{
stud next;
};
stud2 *getnew()
{
stud2 *q;
q= new struct stud2;
return q;
}
int _tmain(int argc, _TCHAR* argv[])
{
stud p;
stud2 *q;
q=getnew();
q->next.id =20;
p=q->next;
cout<<"id= "<<p.id;
getch();
return 0;
}
mrsh_1988
سه شنبه 07 دی 1389, 13:43 عصر
ممنون از پاسخت اما منظورم این نبود.
اینو ببین :
struct stud
{
int id;
};
int _tmain(int argc, _TCHAR* argv[])
{
stud p;
حالا اینجا اشاره گری تعریف کنم که به p اشاره کنه وبا استفاده از این اشاره گر به id مقدار دهی کنم.
getch();
return 0;
}
Arcsinos
سه شنبه 07 دی 1389, 22:58 عصر
فکر میکنم دیگه این درست باشه :
// struct.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
struct stud
{
int id;
};
int _tmain(int argc, _TCHAR* argv[])
{
stud p;
stud *q;
q=new struct stud;
q=&p;
q->id=100;
cout<<p.id ;
getch();
return 0;
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.