Point to pointers problem 
Author Message
 Point to pointers problem

Dear all,
   I am a newbie on C, i am not familiar with the pointer operations.
can anyone give me some suggestion on the following program.  When it is
run, it cause segmentation fault.  Btw, I am using gcc compiler under
linux platform.  Thank a lot!

Chris
___________________________________________________
#include<stdio.h>
#include<stdlib.h>
void str_assign(const char **buffer)

{  strcpy(buffer[0], "hello0");
   strcpy(buffer[1], "hello1");
   return;

Quote:
}

main()
{
   char buffer[3][30];
   str_assign(&buffer);
   printf("buffer[0]=%s\n", buffer[0]);
   printf("buffer[1]=%s\n", buffer[1]);
Quote:
}

____________________________________________


Sat, 21 Apr 2001 03:00:00 GMT  
 Point to pointers problem
/* You are using the wrong types.  Try it this way: */
#include <stdio.h>
#include <string.h>

static void str_assign (char **buffer)
{
  strcpy (buffer[0], "hello0");
  strcpy (buffer[1], "hello1");
  return;

Quote:
}

static char b0[30];
static char b1[30];
static char b2[30];

int main (void)
{
  char *b[3] = {b0, b1, b2};
  char **buffer = b;
  str_assign (buffer);
  printf ("buffer[0]=%s\n", buffer[0]);
  printf ("buffer[1]=%s\n", buffer[1]);
  return 0;

Quote:
}

/* Read the C FAQ to understand why your way did not work */
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Sat, 21 Apr 2001 03:00:00 GMT  
 Point to pointers problem

Quote:

> ___________________________________________________
> #include<stdio.h>
> #include<stdlib.h>
> void str_assign(const char **buffer)

> {  strcpy(buffer[0], "hello0");
>    strcpy(buffer[1], "hello1");
>    return;
> }
> main()
> {
>    char buffer[3][30];
>    str_assign(&buffer);
>    printf("buffer[0]=%s\n", buffer[0]);
>    printf("buffer[1]=%s\n", buffer[1]);
> }

The problem is that you've made the common mistake of assuming pointers
and array references are the same thing.

For buffer, the compiler will make a storage map to store the
2-dimensional array in linear memory.

yx
00 *buffer+1
01 *buffer+2
02
...
10 *buffer+30
11 *buffer+31
12
13

etc

eg: buffer[1][0] (the one in main) will be *(&buffer[0][0] + 1 * 30 + 0)

When you take char **buffer, on the other hand, buffer[0] translates to
*(buffer + 0) and buffer[1] translates to *(buffer + 1).

So now str_assign takes the address of buffer (or the address of the
pointer that points to the storage map) and copies "hello0" over it.
buffer in main now points to garbage.

Even if you'd passed buffer like so: str_assign(buffer), which probably
won't compile, it would take the beginning of the storage map to be a
pointer (eg if char is 8 bits and addresses are 32 bits long, it would
take the values in buffer[0][0], buffer[0][1], buffer[0][2], and
buffer[0][3] and generate a (bad) pointer from those values), and take a
garbage address for strcpy to copy into.

In short, if you want to pass arrays like that, you'll have to at least
declare str_assign as void str_assign(char buffer[][30]) so that it will
do the proper storage map equations.

Also be advised that you should declare main to return type int, and
should have a return at the end of main.  This will make your OS happy.

Cheers!



Sat, 21 Apr 2001 03:00:00 GMT  
 Point to pointers problem


Quote:
>Dear all,
>   I am a newbie on C, i am not familiar with the pointer operations.
>can anyone give me some suggestion on the following program.  When it is
>run, it cause segmentation fault.  Btw, I am using gcc compiler under
>linux platform.  Thank a lot!

You forgot to mention that when you compiled it with gcc the compiler
generated a number of warnings about the code. These warnings are important,
they are telling you that something is wrong (some compiler warnings can be
spurious but not in this case).

Quote:
>Chris
>___________________________________________________
>#include<stdio.h>
>#include<stdlib.h>
>void str_assign(const char **buffer)

This defines str_assign as taking an argument of type pointer to pointer
to const char. That means that the value being passed should be a pointer
to an object that has type pointer to const char, that is a pointer to
a character (which may be part of an array of characters) that this function
won't modify.

Quote:
>{  strcpy(buffer[0], "hello0");
>   strcpy(buffer[1], "hello1");

Immediately here you try to modify these characters so it is wrong to
make the parameter const char.

Quote:
>   return;
>}
>main()

While technically correct this is bad style and will not be correct when
the next revision of the stabdard comes into force in a year or so. Make
that

 int main(void)

Quote:
>{
>   char buffer[3][30];
>   str_assign(&buffer);

buffer is an array of 3 elements where each element is itself an array of
30 chars. &buffer is a pointer to such a datastructure i.e. a
pointer to an array of 3 arrays of 30 chars or char (*)[3][30]. You're
trying to pass this to a function that takes a pointer to pointer to char
(I'll ignore the const). This is a completely different and incompatible
type and what it points at is not a pointer to char (as str_assign
requires) so that function will interpret the object being pointed at
incorrectly.

There are various ways to solve this problem. The simplest is probably to
write here:

    str_assign(buffer);

This is taking the "value" of buffer. C specifies that when you take the
value of an array you get a pointer to its first element. An element of
buffer has type "array of 30 chars" so a pointer to this is a pointer
to an array of 30 chars i.e. char (*)[30]. Therefore the definition of
str_assign above should be:

 void str_assign(char (*buffer)[30])
    .
    .
    .

Quote:
>   printf("buffer[0]=%s\n", buffer[0]);
>   printf("buffer[1]=%s\n", buffer[1]);

main() returns int and returning 0 is an indication of success so

    return 0;

Quote:
>}

--
-----------------------------------------


-----------------------------------------


Sun, 22 Apr 2001 03:00:00 GMT  
 Point to pointers problem
Thankyou very much for the help from all you guys!!

Chris

Quote:



> >Dear all,
> >   I am a newbie on C, i am not familiar with the pointer operations.
> >can anyone give me some suggestion on the following program.  When it is
> >run, it cause segmentation fault.  Btw, I am using gcc compiler under
> >linux platform.  Thank a lot!

> You forgot to mention that when you compiled it with gcc the compiler
> generated a number of warnings about the code. These warnings are important,
> they are telling you that something is wrong (some compiler warnings can be
> spurious but not in this case).

> >Chris
> >___________________________________________________
> >#include<stdio.h>
> >#include<stdlib.h>
> >void str_assign(const char **buffer)

> This defines str_assign as taking an argument of type pointer to pointer
> to const char. That means that the value being passed should be a pointer
> to an object that has type pointer to const char, that is a pointer to
> a character (which may be part of an array of characters) that this function
> won't modify.

> >{  strcpy(buffer[0], "hello0");
> >   strcpy(buffer[1], "hello1");

> Immediately here you try to modify these characters so it is wrong to
> make the parameter const char.

> >   return;
> >}
> >main()

> While technically correct this is bad style and will not be correct when
> the next revision of the stabdard comes into force in a year or so. Make
> that

>  int main(void)

> >{
> >   char buffer[3][30];
> >   str_assign(&buffer);

> buffer is an array of 3 elements where each element is itself an array of
> 30 chars. &buffer is a pointer to such a datastructure i.e. a
> pointer to an array of 3 arrays of 30 chars or char (*)[3][30]. You're
> trying to pass this to a function that takes a pointer to pointer to char
> (I'll ignore the const). This is a completely different and incompatible
> type and what it points at is not a pointer to char (as str_assign
> requires) so that function will interpret the object being pointed at
> incorrectly.

> There are various ways to solve this problem. The simplest is probably to
> write here:

>     str_assign(buffer);

> This is taking the "value" of buffer. C specifies that when you take the
> value of an array you get a pointer to its first element. An element of
> buffer has type "array of 30 chars" so a pointer to this is a pointer
> to an array of 30 chars i.e. char (*)[30]. Therefore the definition of
> str_assign above should be:

>  void str_assign(char (*buffer)[30])
>     .
>     .
>     .

> >   printf("buffer[0]=%s\n", buffer[0]);
> >   printf("buffer[1]=%s\n", buffer[1]);

> main() returns int and returning 0 is an indication of success so

>     return 0;
> >}

> --
> -----------------------------------------


> -----------------------------------------



Mon, 23 Apr 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. the difference between pointers to pointers and point arrays initialization

2. Q: pointers to pointers that point to structs?

3. How to define a pointer that point to a function pointers buffer

4. FLoating points and pointers.

5. array of pointers pointing to functions

6. Increment value pointed to by pointer n

7. Function pointer pointing to a different function type

8. How can I get the size of the memory pointed out by a pointer

9. Legal Pointer re-pointing?

10. Changing a pointer to point to an array of structs

11. Pointers point where??

12. getting sizeof() of what a pointer is pointing at

 

 
Powered by phpBB® Forum Software