pointers

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Having virtually never used any header other than stdio.h I now see on
pg 7 of "C Unleashed" the use of string.h and stdlib.h. If you were a really
good C programmer, would you use macros much? Or just pointers and
variables? I am new to strcpy and I've never used malloc. I've never seen
this free(). What's happening to this char *p? It's being assigned malloc()
and strlen() is used. Alas C has so many shortcuts in programming I don't
know how to keep up. I guess my question is about the p as a pointer to a
char. What is happening to this pointer as you go down through this code?

Bill
 
P

pemo

Bill Cunningham said:
Having virtually never used any header other than stdio.h I now see on
pg 7 of "C Unleashed" the use of string.h and stdlib.h. If you were a
really
good C programmer, would you use macros much? Or just pointers and
variables? I am new to strcpy and I've never used malloc. I've never seen
this free(). What's happening to this char *p? It's being assigned
malloc()
and strlen() is used. Alas C has so many shortcuts in programming I don't
know how to keep up. I guess my question is about the p as a pointer to a
char. What is happening to this pointer as you go down through this code?

Bill - post some code that you're not sure about. I appreciate your
question, but it's hard to answer [for me] without having some code to which
you're addressing a question or two.
 
F

Flash Gordon

Bill said:
Having virtually never used any header other than stdio.h I now see on
pg 7 of "C Unleashed" the use of string.h and stdlib.h. If you were a really
good C programmer, would you use macros much? Or just pointers and
variables? I am new to strcpy and I've never used malloc. I've never seen
this free().

What you will use depends on what you are doing. If you are only writing
embedded software it is entirely possible you won't use malloc and
friends at all, for instance.
> What's happening to this char *p? It's being assigned malloc()
and strlen() is used. Alas C has so many shortcuts in programming I don't
know how to keep up. I guess my question is about the p as a pointer to a
char. What is happening to this pointer as you go down through this code?

You've not shown the code so I've no idea what happens to it.

I seriously suggest you read a beginners book on C and the comp.lang.c
FAQ. One book I would definitely recommend is K&R2 (full reference in
the bibliography of the comp.lang.c FAQ).

This group is no substitute for reading a good text book.
 
M

Mark McIntyre

news:MiLjf.1346$fY3.669@trnddc01...

(stuff)

Its worth pointing out that someone called "Bill Cunningham" has been
postsing similar newby-style questions to CLC for at the last two
years. While its possible its a different person, the style is very
very similar. It feels a bit trollish TBH.
 
M

Mark McIntyre

but it's hard to answer [for me] without having some code to which
you're addressing a question or two.

All you need to do is buy the book... and this surely is topical,
since it was edited by one of the regulars here, and contributed to by
many more.

Page 7 by the way contains one of the most elementary code snippets
you could ever see, in terms of pointers. Really, anyone who can't
understand this needs to buy a completely different C book, this one
is much too advanced for them.
 
B

Bill Cunningham

Bill - post some code that you're not sure about. I appreciate your
question, but it's hard to answer [for me] without having some code to which
you're addressing a question or two.

include stdio.h, string.h, and stdlib.h

int main(void){
char *p /*ok declared*/
#ifdef SCROOGE
p=malloc(strlen("Hello World\n")+1); /*huh?*/
#else /*preprocessor directive */
p=malloc(1000); /* ? */
#endif
if ( p!NULL)
{strcpy(p,"Hello World");
printf("%s\n",p); /* Shouldn't this p be *p */
free(p);}
return 0;}

I haven't compiled this myself yet.

Bill
 
B

Bill Cunningham

Its worth pointing out that someone called "Bill Cunningham" has been
postsing similar newby-style questions to CLC for at the last two
years. While its possible its a different person, the style is very
very similar. It feels a bit trollish TBH.
Yes Mark it's me. One and the same. Just because you might be so great
at C doesn't mean I always have the time to do really in-depth study for a
long while. I haven't posted here in a long time because I've been away from
C for awhile. I know this code may be elementary but I've never had the use
for some of these functions and I think "C Unleased" is a great book. Better
than K&R even. Troll or not troll call me what you like, but I'm just not
that great a C programmer yet.

Bill
 
B

Bill Cunningham

This group is no substitute for reading a good text book.

I guess not. I'll continue reading "C Unleased" as I get time and if I
get stuck I turn to the group. BTW I have k&r2 and I find it as no
substitute to "C Unleased" myself.

Bill
 
B

Bill Cunningham

Really, anyone who can't
understand this needs to buy a completely different C book,

I don't think so.

this one
is much too advanced for them.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----
 
J

Joe Wright

Mark said:
(stuff)

Its worth pointing out that someone called "Bill Cunningham" has been
postsing similar newby-style questions to CLC for at the last two
years. While its possible its a different person, the style is very
very similar. It feels a bit trollish TBH.

Bill Cunningham is not a troll. Bill is one of us, he has been posting
here, off and on, for several years now. Bill suffers a disorder which
trashes his memories. He is of good heart. He might not remember what
happened yesterday.

I am happy to see Bill Cunningham posts. Help him if you can.

Hi Bill, welcome back.
 
R

Richard Heathfield

Bill Cunningham said:
Having virtually never used any header other than stdio.h I now see on
pg 7 of "C Unleashed" the use of string.h and stdlib.h.

I'm sorry about that.
If you were a
really good C programmer, would you use macros much? Or just pointers and
variables? I am new to strcpy and I've never used malloc. I've never seen
this free(). What's happening to this char *p? It's being assigned
malloc() and strlen() is used. Alas C has so many shortcuts in programming
I don't know how to keep up. I guess my question is about the p as a
pointer to a char. What is happening to this pointer as you go down
through this code?

Here's the code from p7 of "C Unleashed" again, this time with very, very,
very detailed comments:

#include <stdio.h> /* prototype for printf */
#include <string.h> /* prototype for strcpy */
#include <stdlib.h> /* prototypes for malloc, free */

int main(void) /* main is a function taking no
* parameters and returning int
*/
{
char *p; /* p is a pointer to a single character BUT at
* present its value is indeterminate because we
* did not point it at anything special.
*/
#ifdef SCROOGE /* conditional compilation */
/* Allocate exactly enough space, including
* one for the '\0'-terminator.
*/
p = malloc(strlen("Hello world") + 1); /* strlen works out the
* length of "Hello world"
* in bytes. This turns out
* out to be eleven, but we
* need one for the null
* character, hence the + 1. We
* pass that result to malloc,
* which allocates (at least) 12 bytes
* of storage and returns a pointer to
* it (or, in the event of failing, it
* will return NULL instead).
*/
..
#else /* if we are less concerned with saving space... */
/* Allocate plenty of space */
p = malloc(1000); /* This function call allocates
* 1000 bytes of space if it succeeds. */
#endif /* end of conditional compilation */

/* note: malloc returns NULL if there
* is not enough memory available.
if(p != NULL) /* p stores the result of the malloc return value */
{
strcpy(p, "Hello world"); /* Copies the string "Hello world" to the
* memory allocated by malloc
*/
printf("%s\n", p); /* p points to memory which contains a
* string. printf copies that string onto
* the standard output stream.
*/
free(p);
/* note: we always free memory when
* we've finished with it.
*/
}

return 0; /* Inform the calling process that all is well. */
}
 
M

Martin Ambuhl

Bill said:
Bill - post some code that you're not sure about. I appreciate your
question, but it's hard to answer [for me] without having some code to
which

you're addressing a question or two.


include stdio.h, string.h, and stdlib.h

int main(void){
char *p /*ok declared*/
#ifdef SCROOGE
p=malloc(strlen("Hello World\n")+1); /*huh?*/
#else /*preprocessor directive */
p=malloc(1000); /* ? */
#endif
if ( p!NULL)
{strcpy(p,"Hello World");
printf("%s\n",p); /* Shouldn't this p be *p */
free(p);}
return 0;}

I haven't compiled this myself yet.

Don't bother. It won't come close to compiling. I hope the following
answers your questions:
#if 0
/* mha: The following line would of course make it impossible for Mr.
Bill to have compiled his code. It is replaced below. */
include stdio.h, string.h, and stdlib.h
#endif
#include <stdio.h> /* mha: replacements */
#include <string.h> /* " */
#include <stdlib.h> /* " */

int main(void)
{
char *p; /* mha: you claim the original 'char
*p' is OK. This is obviously false,
since the terminating ';' was
missing. */
#ifdef SCROOGE
p = malloc(strlen("Hello World\n") + 1);
/* mha: In "Scrooge" only enough space is allocated for the printable
characters of "Hello World", a newline character, and the
terminating null character */
#else
p = malloc(1000); /* mha: when not in "Scrooge" mode, an
excessively large amount of space is
allocated. */
#endif

#if 0
/* mha: the following line is obviously garbage. A replacement
follows. */
if (p ! NULL) {
#endif
if (p) { /* mha: or 'if (p != NULL)' */
strcpy(p, "Hello World");
printf("%s\n", p); /* mha: You ask 'Shouldn't this p be
*p'. Obviously not, since *p is a
single char while p is a pointer to
the (first character of) the string.
*/
free(p);
}
return 0;
}
 
J

John Bode

Bill said:
Bill - post some code that you're not sure about. I appreciate your
question, but it's hard to answer [for me] without having some code to which
you're addressing a question or two.

include stdio.h, string.h, and stdlib.h

#include <stdio.h> /* for printf() */
#include <string.h> /* for strlen() */
#include said:
int main(void){
char *p /*ok declared*/
#ifdef SCROOGE

If the macro SCROOGE is defined, execute the following.
p=malloc(strlen("Hello World\n")+1); /*huh?*/

Allocate a chunk of memory long enough to hold the string "Hello
World\n" plus a 0 terminator, and assign the starting address of that
memory to p.
#else /*preprocessor directive */

If the macro SCROOGE is not defined, execute the following.
p=malloc(1000); /* ? */

Allocate a chunk of memory long enough to hold 1000 chars and assign
the starting address of that memory to p.
#endif
if ( p!NULL)

That should either read

if (p != NULL)

or

if (!p)

If malloc() fails (that is, it cannot allocate the requested amount of
memory), it will return NULL. This test makes sure that malloc()
succeeded.
{strcpy(p,"Hello World");

Copy the string "Hello World" to the memory allocated above.
printf("%s\n",p); /* Shouldn't this p be *p */

No. The %s conversion specifier expects its corresponding argument to
have type char *.
You're telling it what address the string starts at. *p would yield
the value of the first character ('H'), which would not correspond to
a valid address, and the program would likely crash.
free(p);}

Deallocate the memory allocated above.
 
M

Mark McIntyre

On Thu, 01 Dec 2005 23:58:41 GMT, in comp.lang.c , "Bill Cunningham"

I guess what I was gently trying to say was that if, after several
years of trying, you haven't got beyond page 7 of C Unleashed, its not
the book for you.
I'd also suggest that C may not be a sensible language choice. I
carefully refrained from mentioning your medical condition (which
someone else has now brought up) as I consider it your business not
mine, but I feel that under the circumstances, trying to take in C is
makes as much sense as long distance running would be for me. I'm all
for stretching ones boundaries, but some things just aint sensible.
For me thats going up stairs. I know my limitations, I don't push them
beyond whats sensible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top