From: "IVAN DE JESUS DERAS TABORA" <i.deras@unitec.edu>
To: "J." <mailing-lists@xs4all.nl>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: *w[1]++ = *[k[1]++; does not work .. ?
Date: Mon, 24 Feb 2003 08:02:18 -0800 [thread overview]
Message-ID: <3E5A420A.5060203@unitec.edu> (raw)
In-Reply-To: <Pine.LNX.4.21.0302231825450.10716-100000@hestia>
I think this is a common problem when you use arrays in C. When you
declare an array in C, using the following code:
int x[n];
The array will contain indexes from 0 to n-1, then when you declare an
array char *k[1], it contain indexes from 0..0, when you try to access
index 1, is an error because it isn't part of the array!!!!
I hope that this may be helpful.
J. wrote:
>Hello...
>
>I am trying to copy a string into the first element of an array of char
>pointers.
>
>I can print char by char like this: printf("%c", *w[1]++);
>but I can not copy char by char like this: *w[1]++ = *k[1]++;
>
>I dont understand ? If I can print it, it should also copy ..
>
>Strangly enough the compiler does not complain.
>
>I have inserted a small illustrative example in this e-mail.
>
>/* To illustrate the difference: This works */
>#include <stdio.h>
>#include <string.h>
>
>int main(void) {
> char *w[1];
> char str[] = "jehova";
>
> w[1] = strdup(str);
>
> while(*w[1] != '\0')
> printf("%c", *w[1]++);
>
> printf("\n");
> return 0;
>}
>
>/* But this does not work */
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>int main(void) {
> char *w[1];
> char *k[1];
> char str[] = "jehova";
>
> w[1] = strdup(str);
> k[1] = calloc(strlen(str) + 1, sizeof(char));
>
> while(*w[1] != '\0')
> *k[1]++ = *w[1]++;
>
> printf("%s\n", w[1]);
> return 0;
>}
>
>Can someone explain what I am doing wrong ?
>
>Thankx a lot,...
>
>J.
>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
next prev parent reply other threads:[~2003-02-24 16:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-23 17:37 *w[1]++ = *[k[1]++; does not work .. ? J.
2003-02-23 19:30 ` Elias Athanasopoulos
2003-02-24 7:55 ` J.
2003-02-24 9:05 ` Elias Athanasopoulos
2003-02-24 9:35 ` *w[1]++ = solved... thnkx... ? J.
2003-02-24 16:02 ` IVAN DE JESUS DERAS TABORA [this message]
-- strict thread matches above, loose matches on Subject: below --
2003-02-24 8:21 *w[1]++ = *[k[1]++; does not work .. ? Alvarez Alberto-AALVARB1
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3E5A420A.5060203@unitec.edu \
--to=i.deras@unitec.edu \
--cc=linux-c-programming@vger.kernel.org \
--cc=mailing-lists@xs4all.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).