Pointer of Pointers was Pointer of arrays... 
Author Message
 Pointer of Pointers was Pointer of arrays...

Hi again, I need help badly :-(

char *thisone[1][1]={ { "one", "two"}, { "ten", "eleven"} };

this is ok .

but can I assign a value to thisone[1][1] for example ?

thisone[1][1] should store "eleven" right now.

char *thisone_newvalue;

let's assume thisone_newvalue during the program execution
gets a value of "thisone_new_value";

is the following correct ? (I'm sure it is not, that's why I'm asking)

thisone[1][1] = thisone_newvalue;

How can I do to accomplish what said ?

I can't really figure it out.

Thanks.



Wed, 17 Dec 2003 04:53:46 GMT  
 Pointer of Pointers was Pointer of arrays...

Quote:

> Hi again, I need help badly :-(

> char *thisone[1][1]={ { "one", "two"}, { "ten", "eleven"} };

> this is ok .

No its not.  You have declared a 1x1 array of pointers and initialized it as a
2x2 array.  If possible, crank your compiler's diagnostic level so it
complains about this.

Quote:

> but can I assign a value to thisone[1][1] for example ?

Yes, but you lose the pointer to the string literal "eleven". (Of course, with
your declaration thisone[1][1] does not exist).

Quote:

> thisone[1][1] should store "eleven" right now.

No, it has a pointer to the string literal "eleven" (after fixing the
declaration)

Quote:

> char *thisone_newvalue;

> let's assume thisone_newvalue during the program execution
> gets a value of "thisone_new_value";

A pointer thereto.

Quote:

> is the following correct ? (I'm sure it is not, that's why I'm asking)

> thisone[1][1] = thisone_newvalue;

You can assign a pointer to a pointer.
Quote:

> How can I do to accomplish what said ?

> I can't really figure it out.



Wed, 17 Dec 2003 05:14:40 GMT  
 Pointer of Pointers was Pointer of arrays...

Quote:
> Hi again, I need help badly :-(
> char *thisone[1][1]={ { "one", "two"}, { "ten", "eleven"} };
> this is ok .

No it isn't. You cannot assign two elements to one-element arrays.
You want
char *thisone[2][2]={ { "one", "two"}, { "ten", "eleven"} };

Quote:
> but can I assign a value to thisone[1][1] for example ?
> thisone[1][1] should store "eleven" right now.
> char *thisone_newvalue;
> let's assume thisone_newvalue during the program execution
> gets a value of "thisone_new_value";
> is the following correct ? (I'm sure it is not, that's why I'm asking)
> thisone[1][1] = thisone_newvalue;

Of course this is valid. If you have the initialiser I wrote (with
char *thisone[2][2]) and later write:
char *thisone_newvalue="Bow before the mighty Whatzit!";
thisone[1][1] = thisone_newvalue;
then thisone[1][1] will store "Bow before the mighty Whatzit!".

Quote:
> How can I do to accomplish what said ?

By doing what you already are doing.

Quote:
> I can't really figure it out.

You already did.

Quote:
> Thanks.

You're welcome.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"'I' is the most beautiful word in the world."
   - John Nordberg



Wed, 17 Dec 2003 05:07:39 GMT  
 Pointer of Pointers was Pointer of arrays...

Quote:


>> Hi again, I need help badly :-(

>> char *thisone[1][1]={ { "one", "two"}, { "ten", "eleven"} };

>> this is ok .

> No it isn't. You cannot assign two elements to one-element arrays. You
> want
> char *thisone[2][2]={ { "one", "two"}, { "ten", "eleven"} };

Hey wait :-)

printf("%s\n", thisone[0][0]);
printf("%s\n", thisone[0][1]);
printf("%s\n", thisone[1][0]);
printf("%s\n", thisone[1][1]);

one
two
ten
eleven

isn't it ?

as for this one:
char *array[1]="a";

array[0] == 'a';
array[1] == '\0';

Thanks for your help anyway...



Wed, 17 Dec 2003 07:04:22 GMT  
 Pointer of Pointers was Pointer of arrays...

Quote:
> as for this one:
> char *array[1]="a";

> array[0] == 'a';
> array[1] == '\0';

No... when you declare an array, you specify the number of elements, which
is one more than the maximum index.  You would have to say
    char* array[2] = "a";
Because "a" has two characters, 'a' and '\0'.


Wed, 17 Dec 2003 10:24:39 GMT  
 Pointer of Pointers was Pointer of arrays...

Quote:
> > as for this one:
> > char *array[1]="a";

> > array[0] == 'a';
> > array[1] == '\0';

A string literal isn't a valid initializer for array of pointer to char.

Quote:
> No... when you declare an array, you specify the number of elements, which
> is one more than the maximum index.  You would have to say
>     char* array[2] = "a";
> Because "a" has two characters, 'a' and '\0'.

When you _define_ an array, you must either specify
the number of elements, or provide an initializer which
implies it.  If you declare a top-level array (not a member
of another array or struct/union) without defining it,
you can provide the dimension or omit it.  Note that
writing an array declarator for a function parameter
actually declares a pointer, not an array; any dimension
you provide here is ignored, although in C99 apparently
after enforcing the constraints of 6.7.5.2, unless you
use the new C99 [static N] feature.

A string literal used as an initializer for a char array
(or a wide string literal for wchar_t[]) is a special case,
and is treated as the characters which make it up,
followed by a null terminator _if possible_.
  char x[3] = "ab"; /* or */ char x[] = "ab";
both define x as array of three chars, 'a' 'b' '\0'.
  char x[2] = "ab";
defines x as array of two chars 'a' 'b', not null-terminated
and thus not usable with many standard library functions.

char * x[1] = { "ab" }; /* or omit the 1 from the [] */
defines x as an array of one _pointer_ to char,
which is initialized to point to an anonymous
array of three characters 'a' 'b' '\0'.

--
- David.Thompson 1 now at worldnet.att.net



Tue, 23 Dec 2003 13:33:26 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

2. Pointers: return of pointer to array of pointers to main

3. Array of pointers, pointer to array...

4. array pointer/pointer array

5. arrays pointers array of pointers

6. memory leak: pointer->pointer->pointer->struct

7. memory block containing pointers, aka pointer to pointer i believe

8. pointers pointers pointers!!!

9. memory Leak: pointer->pointer->pointer->struct

10. Pointer Functions and Pointers to Pointer Functions

11. Pointer to Pointer to Pointer....

12. Question on pointer-to-pointer-to-pointer

 

 
Powered by phpBB® Forum Software