q about array of char str 
Author Message
 q about array of char str


Quote:

>    strcpy(&(array[n][0]),string); in a loop
>where n and string are variables that already contain valid values.
>It doesn't work.  Can anyone tell me where I'm going wrong.
>thanx

Because array[n][0] degenerates into a pointer, you don't need the ampersand,
'&' character...  This is in K&R2, but I can't seem to recall whether or not
it is in the FAQ listing...  Hope this helps some!  (And I hope for god's sake
that I got this right so as to not incure the wrathe of Dan Pop...  :-)

        - Dan C.



Sat, 14 Dec 1996 10:10:46 GMT  
 q about array of char str

Quote:

>   I'm trying to strcpy into an array of char strings. I declared
> my array like so;
>    char array[50][50];
> because I know up front the size and number of the strings. Now, to
> strcpy into it I do this:
>    strcpy(&(array[n][0]),string); in a loop
> where n and string are variables that already contain valid values.
> It doesn't work.  Can anyone tell me where I'm going wrong.
> thanx

    comp.lang.c is a cruel conference. Either you're right or
    you're wrong. And if you're wrong, you get what you deserve.
    But I'm so bored, I'm gonna risk it.

    The arguments to strcpy are OK. Instead of

        &(array[n][0])

    you could write

        array[n]

    but it's the same thing.
    I guess you are doing something wrong elsewhere.

Slama



Sat, 14 Dec 1996 11:05:14 GMT  
 q about array of char str
Hello C gurus,
        You have help a lot with my previous questions.  Here's
another one:

        I'm trying to strcpy into an array of char strings. I declared
my array like so;
        char array[50][50];
because I know up front the size and number of the strings. Now, to
strcpy into it I do this:
        strcpy(&(array[n][0]),string); in a loop
where n and string are variables that already contain valid values.
It doesn't work.  Can anyone tell me where I'm going wrong.
thanx



Sat, 14 Dec 1996 04:59:21 GMT  
 q about array of char str

Quote:


>>        strcpy(&(array[n][0]),string); in a loop
>>where n and string are variables that already contain valid values.
>>It doesn't work.  Can anyone tell me where I'm going wrong.
>>thanx

>Because array[n][0] degenerates into a pointer, you don't need the ampersand,
>'&' character...  This is in K&R2, but I can't seem to recall whether or not
>it is in the FAQ listing...  Hope this helps some!  (And I hope for god's sake
>that I got this right so as to not incure the wrathe of Dan Pop...  :-)

Sorry, you've got it wrong :-)

array[n][0] produces a char, as everybody knows.

Is it that difficult to test your suggestions with a small program,
before posting them? This is not a 100% sure method of testing advices,
but it helps in many cases to avoid making a fool of yourself.

Cordially,
Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sat, 14 Dec 1996 15:55:17 GMT  
 q about array of char str

Quote:



> >       strcpy(&(array[n][0]),string); in a loop
> >where n and string are variables that already contain valid values.
> >It doesn't work.  Can anyone tell me where I'm going wrong.
> >thanx

> Because array[n][0] degenerates into a pointer, you don't need the ampersand,
> '&' character...  This is in K&R2, but I can't seem to recall whether or not
> it is in the FAQ listing...  Hope this helps some!  (And I hope for god's sake
> that I got this right so as to not incure the wrathe of Dan Pop...  :-)

Maybe Dan's too... for you are *very* wrong on this one. As I recall from the
original posting, array was declared as

  char array[50][50];

so array[n][0] is a char and not an array (it's the first character of the
n+1th string). It will therefore not degenerate into a pointer. There was
simply nothing wrong in the piece of code that Dorian posted. Oh and as to
not recalling whether something is in the faq: why not look it up then?

Btw, Dorian's strcpy() statement could be simplified to

  strcpy (array[n], string);

but some care needs to be taken:

  - is <string.h> #included?
  - is `string' properly null-terminated?
  - is `string''s length 49 or less? (why 49?)
  - is 0 <= n && n < 50 ?

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/


``Look up the words in a dictionary. -- Ian Stewart - Does God Play Dice?
    Mega: big                          (Referring to the use of high-performance
    Flop: failure''                     computers for weather prediction.)



Sat, 14 Dec 1996 18:41:46 GMT  
 q about array of char str


Quote:


>>       strcpy(&(array[n][0]),string); in a loop
>>where n and string are variables that already contain valid values.
>>It doesn't work.  Can anyone tell me where I'm going wrong.
>>thanx

>Because array[n][0] degenerates into a pointer, you don't need the ampersand,

array[n][0] has type char so it doesn't degenerate into anything, and the &
is essential.

Quote:
>'&' character...  This is in K&R2, but I can't seem to recall whether or not
>it is in the FAQ listing...  Hope this helps some!  (And I hope for god's sake
>that I got this right so as to not incure the wrathe of Dan Pop...  :-)

Don't worry - I get there first! :-)

As noted in another posting &(array[n][0]) (parentheses not actually
necessary since [] has higher precedence than &) is equivalent to array[n].
array[n] has type 'array of char' which in an expression context (such as here)
has the value of a pointer to the first character in the array, i.e. it is
the same as &array[n][0].

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


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



Sat, 14 Dec 1996 19:50:18 GMT  
 q about array of char str

Quote:

>Sorry, you've got it wrong :-)

Oh well, Maybe my reply got to a few news servers first! :-)

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


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



Sun, 15 Dec 1996 00:41:13 GMT  
 q about array of char str

Quote:

>Sorry, you've got it wrong :-)
>array[n][0] produces a char, as everybody knows.

You're absolutely right, I must have been delerious at the time...
(yikes...)

Quote:
>Is it that difficult to test your suggestions with a small program,
>before posting them? This is not a 100% sure method of testing advices,
>but it helps in many cases to avoid making a fool of yourself.

No, not at all...  That is what I plan to do before posting ever again.
As someone else pointed out also, I should have checked the FAQ more
carefully instead of jumping in and saying, "Well, I can't remember exactly,
but I *think* it says this..."

Quote:
>Cordially,

hehehe...  Definately the politest flame I've ever received...

As I said to Steve Summit who sent me mail, "Please don't let Dan Pop moderate
the FAQ, I think that he would scare people to death..."  :-)

Now, if you will all excuse me, I will go bury my head in the sand with my
tail between my legs...

        - Dan C.



Sun, 15 Dec 1996 10:59:42 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char *str vs. char s

4. char* and char str[2]

5. c standard regarding char str[ ], str[50] & *str

6. Differences between char array[SIZE] and char *array

7. Converting char array of literals chars to escape chars

8. A char pointer (char *) vs. char array question

9. Basic Question - char *str V/S int *

10. str != (char *) NULL

11. register char str[LEN]

12. how to address char * str[] ?

 

 
Powered by phpBB® Forum Software