473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to access pointer inside a structure pointer

Hello friends,

struct datas{
int *a,*b;
} *name;

can you explain me how can i get the data at location pointed by *a and *b.

i tried,

name->*a = 10; // but this produces an error...
name->*b = 5; // but this produces an error...

Thanks...
H.Mohamed Thalib
Sep 15 '07 #1
7 32290
JosAH
11,448 Expert 8TB
I'll move your question to the C/C++ forum section where it belongs.

kind regards,

Jos
Sep 15 '07 #2
ilikepython
844 Expert 512MB
Hello friends,

struct datas{
int *a,*b;
} *name;

can you explain me how can i get the data at location pointed by *a and *b.

i tried,

name->*a = 10; // but this produces an error...
name->*b = 5; // but this produces an error...

Thanks...
H.Mohamed Thalib
You need to dereference it in front:
Expand|Select|Wrap|Line Numbers
  1. *name->a = 10;
  2.  
Use parenthesis to make it clearer:
Expand|Select|Wrap|Line Numbers
  1. *(name->a) = 10;
  2.  
Sep 15 '07 #3
JosAH
11,448 Expert 8TB
Even more important: a pointer must point to something; in the OPs example
the name pointer doesn't point to a struct with two int pointers so nothing can
be dereferenced at all.

kind regards,

Jos
Sep 15 '07 #4
You need to dereference it in front:
Expand|Select|Wrap|Line Numbers
  1. *name->a = 10;
  2.  
Use parenthesis to make it clearer:
Expand|Select|Wrap|Line Numbers
  1. *(name->a) = 10;
  2.  

Thank you guys ... i t works,....
Sep 17 '07 #5
sajet
3
try this

name = (struct datas *)malloc(sizeof *name);

now u can access

name->a = 10;
name->b = 5;
Nov 8 '08 #6
TamusJRoyce
110 100+
Close Sajet... But your allocation only allocates space for an unsigned integer/pointer, since name is a pointer. You need the sizeof(datas).

name = (struct datas *)malloc(sizeof(datas))

Now that you have a valid datas allocated to name, you need *a and *b to be something.

// Always check to see if name truely has been allocated.
// The system could be out of memory.
if (name != NULL)
{
name->a = (int)malloc(sizeof(int))
name->b = (int)malloc(sizeof(int))

if (name->a != NULL)
{
*name->a = 5

// Done using it, so lets free it
free(name->a)
} // End if

if (name->b != NULL)
{
*name->b = 10

// Done using it, so lets free it
free(name->b)
} // End if

// Done using it, so lets free it
free(name)
} // End if

Search how pointers work with the below if you're interested...
1. Dynamic Arrays
2. Polymorphism

1. But dynamic memory allocation is exciting/useful when you get into linked lists and dynamic arrays.

2. It's also useful in C++ when dynamically allocating a class which inherits a base class (using new/delete instead of malloc/free, of course), and that class can be set to a base class pointer with that class abstractly performing like the base class, but doing much more stuff in the extended part.

Hopefully helpful,

TamusJRoyce
Nov 8 '08 #7
Banfa
9,065 Expert Mod 8TB
Close Sajet... But your allocation only allocates space for an unsigned integer/pointer, since name is a pointer. You need the sizeof(datas).
Not true notice Sajet wrote

name = (struct datas *)malloc(sizeof *name);

not

name = (struct datas *)malloc(sizeof name);

since *name is a struct of type datas sizeof *name returns the size of the struct name points to. In fact for me

name = malloc(sizeof *name);

is preferable to

name = malloc(sizeof (datas));

because if the type of name is changed (which I admit is notterribly likely) the first form of the malloc still works without alteration where as the second requires a change to the sizeof to work correctly. For any type T using

T *pointer;
pointer = malloc(sizeof *pointer);

is about the most robust way you can write the malloc. NOTE I have removed the casts because they are not necessary (in fact they are dangerous) in C and if you are using C++ you should be using new anyway to ensure and constructors get called.
Nov 8 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: Don Pasquale | last post by:
The following function intends to delete "numberoflines" lines from a text file, named "s" (string pointer) and pointed to by file pointer "fp", starting from line "line". Now, the function...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
7
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.