473,654 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

contents of what a pointer points to??

JS
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"? ?

JS
Nov 14 '05 #1
11 1860
JS wrote:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"? ?


It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.
Nov 14 '05 #2
> static char *allocp = allocbuf;
^^^^^^^^^
If you look more careful you'll see that allocp has been set to the
start of the array allocbuf. allocbuf is basicly a pointer to the
memorylocation for the array.

--
bjrnove

Nov 14 '05 #3
JS

"Mark Odell" <od*******@hotm ail.com> skrev i en meddelelse
news:39******** *****@individua l.net...
JS wrote:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible to make the contents of an unknown address be "allocbuf"? ?


It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.


But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is
the first element of allocbuf.

JS
Nov 14 '05 #4
JS wrote:
"Mark Odell" <od*******@hotm ail.com> skrev i en meddelelse
news:39******** *****@individua l.net...
JS wrote:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it
possible
to make the contents of an unknown address be "allocbuf"? ?


It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.

But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is
the first element of allocbuf.


Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */

Do not confuse this with assignment (not the same as initialization)
like this:

*pAllocbuf = allocbuf; /* Error! */

Okay?

- Mark
Nov 14 '05 #5
On Wed, 16 Mar 2005 15:52:23 +0100, JS wrote:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"? ?


Your second line has an initialiser for allocp. It is saying that allocp
is a static variable of type char * and its initial value is (a
pointer to the first element of) allocbuf. The initialisation is broadly
equivalent to the assignment (executed sometime before the program starts):

allocp = allocbuf;

it is NOT equivalent to

*allocp = allocbuf;

Lawrence
Nov 14 '05 #6
JS

"Mark Odell" <od*******@hotm ail.com> skrev i en meddelelse
news:39******** *****@individua l.net...
JS wrote:
"Mark Odell" <od*******@hotm ail.com> skrev i en meddelelse
news:39******** *****@individua l.net...
JS wrote:

In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it


possible
to make the contents of an unknown address be "allocbuf"? ?

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.

But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is the first element of allocbuf.


Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */


This last line implies pAllocbuf = allocbuf..right ?

But is it the value at allocbuf[0] or is it the address?
Nov 14 '05 #7
JS wrote:
>In K&R I have found this:
>
>static char allocbuf[ALLOCSIZE];
>static char *allocp = allocbuf;
>
>But *allocp has not been set to point at anything yet so how is it

possible
>to make the contents of an unknown address be "allocbuf"? ?

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.
But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at
is
the first element of allocbuf.
Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */

This last line implies pAllocbuf = allocbuf..right ?


It doesn't imply it, C says that it will initialize pAllocbuf to point
to allocbuf just as if you had written:

pAllocbuf = allocbuf;

So I guess your statement is correct.
But is it the value at allocbuf[0] or is it the address?


pAllocbuf will contain the address of allocbuf[0], e.g. &allocbuf[0] and
*pAllocbuf will contain the value held in allocbuf[0].

--
- Mark
Nov 14 '05 #8
JS

"Lawrence Kirby" <lk****@netacti ve.co.uk> skrev i en meddelelse
news:pa******** *************** *****@netactive .co.uk...
On Wed, 16 Mar 2005 15:52:23 +0100, JS wrote:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible to make the contents of an unknown address be "allocbuf"? ?
Your second line has an initialiser for allocp. It is saying that allocp
is a static variable of type char * and its initial value is (a
pointer to the first element of) allocbuf. The initialisation is broadly
equivalent to the assignment (executed sometime before the program

starts):
allocp = allocbuf;

it is NOT equivalent to

*allocp = allocbuf;

In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];

with I assume is also equivalent with:

static char allocp = allocbuf;
Nov 14 '05 #9
"JS" <sf****@asdas.c om> writes:
In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];
Yes. If allocbuf is an array of char or a pointer to char, both
are valid and equivalent.
with I assume is also equivalent with:

static char allocp = allocbuf;


No. Given the same assumption, this code requires a diagnostic
because it implicitly converts from a pointer type to a character
type.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
Nov 14 '05 #10

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

Similar topics

3
1372
by: Guybrush Threepwood | last post by:
double ** matrix; struct data_point { float x,y,z; float nx,ny,nz; float value; };
10
1626
by: houstorx | last post by:
I've been looking at the committee draft of the C99 specification, specifically the one at this URI: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n843.pdf. I don't have a copy of the official international standard, but I assume that it is similar. The rules for pointer arithmetic (6.5.6.7-8) are appended below for ease of reference. They seem astonishingly restrictive. As far as I can see, there is no guarantee that the code:
10
11110
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel uneasy leaving it hanging in memory indefinitely (especially in case when string is Interned). So at leats for the case when string is not interned i propose:
23
3439
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
8
2396
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
42
5312
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
5
9845
by: barcaroller | last post by:
Is there a way of getting a C-style pointer to the contents of a vector<T> (similar to string's c_str)? If not, what is the most efficent way of doing this? The reason I need a C-style pointer is because I need to map the contents of the vector<Tto a C-style struct (for reading, not writing).
12
2551
by: Michael Bell | last post by:
I am trying to put my learning effort into the most useful things. What do pointers do that other things can't? Michael Bell --
11
4354
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some mistakes, i'm using cygwin on a windows pc and it seems that some functions cause stack overflows so am using fgets instead of fgetc (don't know y it solved the problem but that part is working). my problem now is that i am reading strings a few...
0
8285
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,...
1
8475
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
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1592
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.