473,671 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A little rusty on pointers to pointers

Sam
Hi there. I hope this code is standard C. I think it is. Anyway, I
haven't used C in a Looooooong time and am a little rusty on pointers.
I am wondering why "buff" is a pointer to a pointer instead of just
"*buff". I'm also not really sure about the language of pointers. Is
it correct to say "pointer to a pointer to buff" or "double pointer to
buff" or what? If you can help me with this, you have my thanks...

REM --- *Code Below* ---

void LoadFileIntoMem ory(const char *name, void **buff, int *length)
{
FILE *fp = fopen(name, "rb");

fseek(fp, 0, SEEK_END);
*length = ftell(fp);
fseek(fp, 0, SEEK_SET);

*buff = malloc(*length) ;
fread(*buff, *length, 1, fp);

fclose(fp);
}

REM --- *End Code* ---
Sep 24 '08 #1
1 1625
Sam <sa*@iam.netwri tes:
Hi there. I hope this code is standard C. I think it is. Anyway, I
haven't used C in a Looooooong time and am a little rusty on
pointers. I am wondering why "buff" is a pointer to a pointer instead
of just "*buff". I'm also not really sure about the language of
pointers. Is it correct to say "pointer to a pointer to buff" or
"double pointer to buff" or what? If you can help me with this, you
have my thanks...

REM --- *Code Below* ---

void LoadFileIntoMem ory(const char *name, void **buff, int *length)
{
FILE *fp = fopen(name, "rb");

fseek(fp, 0, SEEK_END);
*length = ftell(fp);
fseek(fp, 0, SEEK_SET);

*buff = malloc(*length) ;
fread(*buff, *length, 1, fp);

fclose(fp);
}

REM --- *End Code* ---
As you probably know, but I'll emphasize it anyway, all function
arguments are passed by value. A parameter is a local variable,
containing (at least initially) a copy of the argument value.
Modifying the parameter object (name, buff, or length in this case)
only modifies this local variable, which ceases to exist when the
function returns.

Consider a call to this function:

void *buff;
int length;
LoadFileIntoMem ory("foo.dat", &buff, &length);

The function passes back two pieces of information to the caller: the
location where it stored the data, and the length of the data.

The location of the data is represented as a void*; the length of the
data is represented as an int. So the caller has to pass in *a
pointer to* a void*, and *a pointer to* an int, so that the function
knows where to store the void* and int values that the caller needs.

Suppose the arguments were just a void* and an int:

void LoadFileIntoMem ory(..., void **buff, int length);
{
length = ftell(fp);
...
buff = malloc(length);
}

Then the values of length and buff would be lost.

As for terminology, "pointer to pointer to void" is fine (it's not a
pointer to pointer to buff; buff *is* the pointer). The term "double
pointer" should be avoided, unless you're talking about a double*
(i.e., a pointer to a floating-point object).

As for the function itself:

There is no error checking, and no mechanism for the function to
report an error to its caller. What if fopen() fails for any of a
variety of reasons? What if fseek() fails because you passed it the
name of the keyboard device? What if malloc fails because you've run
out of memory? Likewise for fread and fclose.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 24 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3474
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class dt { std::deque< class dt > dtdq; };
1
1073
by: Paul | last post by:
hello, My application is built in VB6 and i'd like to upgrade to VB.NET and make some fairly large changes at the same time. The application writes a script (simular to VBScript) for the user based on their selection from combo boxes and entries into text boxes etc, the script is then used in a seperate application. The app basically makes it easy for someone who cannot code to create that code from english language expressions.
54
2974
by: ash | last post by:
i am writing this program (for exercise1-9 in k&r-2nd edition) which removes extra spaces in string example- "test string" will be "test string" #include<stdio.h> #include<string.h> #include<conio.h> main() { char str,st;
3
1617
by: David Hearn | last post by:
I've been doing ASP.NET for the past few years and have gotten spoiled and now find myself in a situation where I need to make some changes to an old classic ASP page. The problem is that there is a VB script function that populates an array. There are some dropdown boxes on the page that cause the page to reload. When it does reload, it causes the function that loads the array to fire again and I don't want it to. I want to hold the...
52
3366
by: ¬a\\/b | last post by:
is it possible that a malloc function that use a nobody student is better than all yours :) How many errors do you see? Bug1: memory<13MB Bug2: it seems do you have to write you own sprintf "Ps_m" Ps_m(char* out, int len, char* fmt, ...)
13
1706
by: Jack B | last post by:
I'm using Access 2002 to create a database for a small opera company that my wife is involved in, and I'm more than a bit rusty because I haven't created a new Access database since about 1999. So, I will probably have quite a few questions as I go through this, and hopefully this group will help me out. I will be creating this database for someone else to enter the data. I have a patron table with name, address, email, etc., and I...
46
6248
by: C C++ C++ | last post by:
what is an escaping variable?
23
1391
by: Raz | last post by:
On Wed, 30 Apr 2008 21:54:37 GMT, Erik Wikströ As I said, I will find the link again. His knowledge of C++ is just fine. http://www.builderau.com.au/video/soa/Rusty-s-message-to-C-programmers/0,2000064338,22432924p,00.htm http://www.builderau.com.au/video/soa/Why-C-remains-relevant/0,2000064338,22432921p,00.htm
23
7036
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.