[comp.lang.c] pointers - why dosn't this work?

aryeh@eddie.mit.edu (Aryeh M. Weiss) (04/05/90)

In article <23467@mimsy.umd.edu> dbk@mimsy.umd.edu (Dan Kozak) writes:
>From article <6067@ozdaltx.UUCP>, by root@ozdaltx.UUCP (root):
>> This is driving me crazy!  According to the C books, this should work,
>> but yet I get core dumps when I compile and run it.  Suggestions???
>
>Sell that book.
>
>> #include <stdio.h>
>> #include <string.h>
>> main()
>> {
>>       char b[1][20];
>>       char *x="This is a test";
>>       char *a[3];
>	     ^^^^^ this is a pointer to an array of 3 chars, but the
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No way, Jose', this is an array of 3 pointers of type char.
(A pointer to an array is just a pointer.)

>		   array(s) that it points to have not been allocated
>		   and it hasn't itself been initialized.
>	
>Clever:         dbk@mimsy.umd.edu | "For I was rolled in water,
>Not-so-clever:  uunet!mimsy!dbk   |  I was rolled out past the pier" - MoB

-------------
``I must go.  Somewhere a bug is happening.''
-- 

rogerk@sco.COM (Roger Knopf 5502) (04/06/90)

In article <23467@mimsy.umd.edu> dbk@mimsy.umd.edu (Dan Kozak) writes:
>From article <6067@ozdaltx.UUCP>, by root@ozdaltx.UUCP (root):
>> #include <stdio.h>
>> #include <string.h>
>> main()
>> {
>>       char b[1][20];
>>       char *x="This is a test";
>>       char *a[3];
>	     ^^^^^ this is a pointer to an array of 3 chars, but the
>		   array(s) that it points to have not been allocated
>		   and it hasn't itself been initialized.

Your concept is right but it is actually an array of 3 pointers to
char. The result is still the same - these pointers are unitialized
and point to who-knows-what.

Roger Knopf
SCO Consulting Services
-- 

"His potential clients were always giving him the business."
	--Robert Thornton

rogerk@sco.COM (Roger Knopf 5502) (04/06/90)

In article <23474@mimsy.umd.edu> dbk@mimsy.umd.edu (Dan Kozak) writes:

There is an even easier way

>Actually it is 3 uninitialized pointers.  The first fix still works,
>but if you want to keep a declared as in the original, add 
>
>      a[0] = (char *) malloc(sizeof(x) + 1);     /* this */
>      strcpy(a[0], x);
Instead of the preceding two lines, use:
	a[0] = strdup(x);

strdup(S) automagically does the malloc for you and returns a 
pointer to where it duped the string.

Roger Knopf
SCO Consulting Services
-- 

"His potential clients were always giving him the business."
	--Robert Thornton

henry@utzoo.uucp (Henry Spencer) (04/11/90)

In article <4135@scorn.sco.COM> rogerk@sco.COM (Roger Knopf 5502) writes:
>strdup(S) automagically does the malloc for you and returns a 
>pointer to where it duped the string.

Assuming that you have a strdup() function.  It's not particularly standard.
-- 
Apollo @ 8yrs: one small step.|     Henry Spencer at U of Toronto Zoology
Space station @ 8yrs:        .| uunet!attcan!utzoo!henry henry@zoo.toronto.edu

rogerk@sco.COM (Roger Knopf 5502) (04/12/90)

In article <1990Apr10.181613.5367@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <4135@scorn.sco.COM> rogerk@sco.COM (Roger Knopf 5502) writes:
>>strdup(S) automagically does the malloc for you and returns a 
>>pointer to where it duped the string.
>
>Assuming that you have a strdup() function.  It's not particularly standard.

I made the assumption that it was available because I was reading it
in comp.unix.xenix, not noticing the cross-post to comp.lang.c. I
don't know whether its standard or not but it is available in the SCO
libc. Sorry for any confusion this may have caused anybody.

Roger Knopf
SCO Consulting Services
-- 

"His potential clients were always giving him the business."
	--Robert Thornton