Sponsored Content
Top Forums Programming Using pointers to struct members as args to functions Post 302542591 by uiop44 on Thursday 28th of July 2011 04:24:06 AM
Old 07-28-2011
Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf.

Anyway, here's a similar example, to illustrate the technique:

Code:
struct a *pp;

struct a b;
b.1 = c; 
b.2 = cc; 
b.3 = ccc; 
b.4 = cccc; 
}

struct a b, *pp;

pp = &b;

printf("b.1 is %s\n",(*pp).b.1);

This is easy enough to understand.

But how do we do this with structure members that are themselves pointers or functions?

Let's say I have a struct that resembles the one below. And I want to print the value of lg.

Code:
const struct kelly {
const char *cindy;
const char *juliette;
void (*anna)(const char *);
void (*jennifer)(void);
void (*rachel)(void);
void (*lg)(int);
char *(*christine)(const char *, char *);
}

 

9 More Discussions You Might Find Interesting

1. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

2. Homework & Coursework Questions

C++ struct pointers & functions

Hi All, My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
Discussion started by: pondlife
0 Replies

3. Programming

functions that manipulate void pointers

I have two or more linked lists that have the same names for their "next". For example, struct server_t { sockaddr_in * sin; server_t * next_; } struct player_t { char name; player_t * next_; } How can I get a function to take in either type and manipulate the pointers? I... (3 Replies)
Discussion started by: pyramation
3 Replies

4. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

5. Homework & Coursework Questions

Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Discussion started by: ignatius3
2 Replies

6. Programming

Passing an instance of struct to functions in other src files

I am trying to work out the best syntax for a relatively simple operation. The goal is to declare an instance of a struct and pass it around to be populated and have the data manipulated. There is an extra wrinkle in that the functions are in different src files. The main is simple, #include... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

7. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

8. Programming

Dynamically enumerating the members of a C++ struct

In C++ there is a struct with the following structure: typedef struct myStruct { string s_date; float fA; float fB; float fC; ..... float fZ; } myData; After some computations on the values of the struct members are inserted into a database table: myData... (4 Replies)
Discussion started by: figaro
4 Replies

9. Shell Programming and Scripting

Store args passed in array but not the first 2 args

Store args passed in array but not the first 2 args. # bash declare -a arr=("$@") s=$(IFS=, eval 'echo "${arr}"') echo "$s" output: sh array.sh 1 2 3 4 5 6 1,2,3,4,5,6 Desired output: sh array.sh 1 2 3 4 5 6 3,4,5,6 (2 Replies)
Discussion started by: iaav
2 Replies
ARCHIVE_ENTRY_PATHS(3)					   BSD Library Functions Manual 				    ARCHIVE_ENTRY_PATHS(3)

NAME
archive_entry_hardlink, archive_entry_hardlink_w, archive_entry_set_hardlink, archive_entry_copy_hardlink, archive_entry_copy_hardlink_w, archve_entry_update_hardlink_utf8, archive_entry_set_link, archive_entry_copy_link, archive_entry_copy_link_w, archve_entry_update_link_utf8, archive_entry_pathname, archive_entry_pathname_w, archive_entry_set_pathname, archive_entry_copy_pathname, archive_entry_copy_pathname_w, archve_entry_update_pathname_utf8, archive_entry_sourcepath, archive_entry_copy_sourcepath, archive_entry_symlink, archive_entry_symlink_w, archive_entry_set_symlink, archive_entry_copy_symlink, archive_entry_copy_symlink_w, archve_entry_update_symlink_utf8 -- functions for manip- ulating path names in archive entry descriptions LIBRARY
Streaming Archive Library (libarchive, -larchive) SYNOPSIS
#include <archive_entry.h> const char * archive_entry_hardlink(struct archive_entry *a); const wchar_t * archive_entry_hardlink_w(struct archive_entry *a); void archive_entry_set_hardlink(struct archive_entry *a, const char *path); void archive_entry_copy_hardlink(struct archive_entry *a, const char *path); void archive_entry_copy_hardlink_w(struct archive_entry *a, const, wchar_t, *path"); int archive_entry_update_hardlink_utf8(struct archive_entry *a, const char *path); void archive_entry_set_link(struct archive_entry *a, const char *path); void archive_entry_copy_link(struct archive_entry *a, const char *path); void archive_entry_copy_link_w(struct archive_entry *a, const wchar_t *path); int archive_entry_update_link_utf8(struct archive_entry *a, const char *path); const char * archive_entry_pathname(struct archive_entry *a); const wchar_t * archive_entry_pathname_w(struct archive_entry *a); void archive_entry_set_pathname(struct archive_entry *a, const char *path); void archive_entry_copy_pathname(struct archive_entry *a, const char *path); void archive_entry_copy_pathname_w(struct archive_entry *a, const wchar_t *path); int archive_entry_update_pathname_utf8(struct archive_entry *a, const char *path); const char * archive_entry_sourcepath(struct archive_entry *a); void archive_entry_copy_sourcepath(struct archive_entry *a, const char *path); const char * archive_entry_symlink(struct archive_entry *a); const wchar_t * archive_entry_symlink_w(struct archive_entry *a); void archive_entry_set_symlink(struct archive_entry *a, const char *path); void archive_entry_copy_symlink(struct archive_entry *a, const char *path); void archive_entry_copy_symlink_w(struct archive_entry *a, const wchar_t *path); int archive_entry_update_symlink_utf8(struct archive_entry *a, const char *path); DESCRIPTION
Path names supported by archive_entry(3): hardlink Destination of the hardlink. link Update only. For a symlink, update the destination. Otherwise, make the entry a hardlink and alter the destination for that. pathname Path in the archive sourcepath Path on the disk for use by archive_read_disk(3). symlink Destination of the symbolic link. Path names can be provided in one of three different ways: char * Multibyte strings in the current locale. wchar_t * Wide character strings in the current locale. The accessor functions are named XXX_w(). UTF-8 Unicode strings encoded as UTF-8. This are convience functions to update both the multibyte and wide character strings at the same time. The sourcepath is a pure filesystem concept and never stored in an archive directly. For that reason, it is only available as multibyte string. The link path is a convience function for conditionally setting hardlink or sym- link destination. It doesn't have a corresponding get accessor function. archive_entry_set_XXX() is an alias for archive_entry_copy_XXX(). SEE ALSO
archive(3), archive_entry(3) BSD
February 2, 2012 BSD
All times are GMT -4. The time now is 01:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy