473,397 Members | 2,084 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,397 software developers and data experts.

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 1840
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*******@hotmail.com> skrev i en meddelelse
news:39*************@individual.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*******@hotmail.com> skrev i en meddelelse
news:39*************@individual.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*******@hotmail.com> skrev i en meddelelse
news:39*************@individual.net...
JS wrote:
"Mark Odell" <od*******@hotmail.com> skrev i en meddelelse
news:39*************@individual.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****@netactive.co.uk> skrev i en meddelelse
news:pa****************************@netactive.co.u k...
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.com> 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
"JS" <sf****@asdas.com> writes:
"Lawrence Kirby" <lk****@netactive.co.uk> skrev i en meddelelse
news:pa****************************@netactive.co.u k...
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;
[snip] 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];


Yes.
with I assume is also equivalent with:

static char allocp = allocbuf;


No, that's not equivalent at all. The first two declarations declare
allocp as a variable of type pointer-to-char. The last declares is as
a variable of type char, a completely different type.

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/faq.html>.
I recommend section 6, which discusses arrays and pointers. (Actually
I recommend the whole thing, but section 6 is most relevant to what
you're asking about.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
JS wrote:

<snip>
In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];
Yes. That is because the name of an array degenerates to a pointer to
it's first element and, obviously, the address of the first element is a
pointer to the first element.
with I assume is also equivalent with:

static char allocp = allocbuf;


Why would you assume that? "char *allocp" is a pointer to char, "char
allocp" is a char. Whether you initialise it at the same time has
nothing to do with it. So

static char allocp = allocbuf;

Is an error when allocbuf is an array.

I suggest you reread more carefully what your book says about declaring
variables and pointer and then reread what it says about initialisers.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #12

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

Similar topics

3
by: Guybrush Threepwood | last post by:
double ** matrix; struct data_point { float x,y,z; float nx,ny,nz; float value; };
10
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...
10
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...
23
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
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...
42
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
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...
12
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.