A beginner question - Urgent need help.
Author Message
A beginner question - Urgent need help.

I am a beginner
If i want to put this 5 letters from Freq[5] to new a array within one
element, how to do?
How to create array  "fiveletter"

Freq[0]=A
Freq[1]=A
Freq[2]=A
Freq[3]=A
Freq[4]=A
>                fiveletter[0]=AAAAA

Thanks
Reala

Tue, 05 Apr 2005 14:49:18 GMT
A beginner question - Urgent need help.
Reala's question was (translated to English):

Quote:
> I am a beginner with a simple question to ask. If I want
> to put the 5 letters from Freq[5] into a new array within
> one element, how can I do this? How should I declare the
> array "fiveletter"?

> Freq[0]=A
> Freq[1]=A
> Freq[2]=A
> Freq[3]=A
> Freq[4]=A
>    >                fiveletter[0]=AAAAA

freq is an array of 6 char, containing the string "AAAAA".
fiveletter is an array of 1 pointer to char, whose first element contains a
pointer to the first element of freq.

#include <stdio.h>

int main(void)
{
char freq[] = "AAAAA";
char *fiveletter[1];
fiveletter[0] = freq;

printf("freq[0] = %c\n", freq[0]);
printf("freq = %s\n", freq);
printf("fiveletter[0] = %s\n", fiveletter[0]);
printf("fiveletter[0][0] = %c\n", fiveletter[0][0]);
return 0;

Quote:
}

--
Simon.

Tue, 05 Apr 2005 15:35:02 GMT
A beginner question - Urgent need help.

Quote:
> Reala's question was (translated to English):
>> I am a beginner with a simple question to ask. If I want
>> to put the 5 letters from Freq[5] into a new array within
>> one element, how can I do this? How should I declare the
>> array "fiveletter"?

>> Freq[0]=A
>> Freq[1]=A
>> Freq[2]=A
>> Freq[3]=A
>> Freq[4]=A
>>    >                fiveletter[0]=AAAAA

> freq is an array of 6 char, containing the string "AAAAA".
> fiveletter is an array of 1 pointer to char, whose first element contains a
> pointer to the first element of freq.

> #include <stdio.h>

> int main(void)
> {
>   char freq[] = "AAAAA";
>   char *fiveletter[1];

But if a new copy needs to made of the string, this should be

char fiveletter[1][sizeof freq];

Quote:
>   fiveletter[0] = freq;

strcpy(fiveletter[0], freq);

Quote:

>   printf("freq[0] = %c\n", freq[0]);
>   printf("freq = %s\n", freq);
>   printf("fiveletter[0] = %s\n", fiveletter[0]);
>   printf("fiveletter[0][0] = %c\n", fiveletter[0][0]);

Where
printf("fiveletter[0] = %p\n", (void *)fiveletter[0]);
would give different results for our examples, but the above
outputs would be equal.

Quote:
>   return 0;
> }

> --
> Simon.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond

Tue, 05 Apr 2005 16:42:47 GMT
A beginner question - Urgent need help.

Quote:

> I am a beginner
> A simple question to ask..
> If i want to put this 5 letters from Freq[5] to new a array within one
> element, how to do?
> How to create array  "fiveletter"

> Freq[0]=A
> Freq[1]=A
> Freq[2]=A
> Freq[3]=A
> Freq[4]=A
>    >                fiveletter[0]=AAAAA

> Thanks
> Reala

If I understand well what you want : Freq is a array of letters, and
you want to create an array whose first element is the set of Frequ's
chars all-in-one.

Note : 'letter' does not exist in C, I suppose what you want is char.

Ugly solution :

****************************

#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
char Freq[5] = "AAAAA";
char fiveletters[1][5];
strncpy(fiveletters[0], Freq, 5*sizeof(char));

printf("Freq=%s\nfiveletters=%s\n", Freq, fiveletters[0]);

return 0;

Quote:
}

****************************

As you can see, the output is quite ugly.
That is because this solution does not use strings (including the
terminating `\0' character).

Better one :

****************************

#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
char Freq[6] = "AAAAA\0";
char fiveletters[1][6];
strcpy(fiveletters[0], Freq);

printf("Freq=%s\nfiveletters=%s\n", Freq, fiveletters[0]);

return 0;

Quote:
}

*****************************

Last one :

*****************************

#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
char Freq[6];

Freq[0]='A';
Freq[1]='A';
Freq[2]='A';
Freq[3]='A';
Freq[4]='A';
Freq[5]='\0';

printf("Freq=%s\n", Freq);

return 0;

Quote:
}

*********************************

I suggest you to get some C tutorial and read carrefully the chapter

I hope this will help.
Xebax

Tue, 05 Apr 2005 19:05:59 GMT
A beginner question - Urgent need help.

<snip>

Quote:
> Ugly solution :

> ****************************

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char * argv[])
> {
>   char Freq[5] = "AAAAA";

"AAAAA" is a string.
Freq is not a string - no terminating null character.

Quote:
>   char fiveletters[1][5];
>   strncpy(fiveletters[0], Freq, 5*sizeof(char));

fiveletters[0] is not a string.

Quote:

>   printf("Freq=%s\nfiveletters=%s\n", Freq, fiveletters[0]);

Undefined behaviour x 2, since the %s specification expects a
string.

Quote:

>   return 0;
> }

> ****************************

> As you can see, the output is quite ugly.

Is it? When I ran this program, it drew 3D-fractals on my screen.
;-)

Quote:
> That is because this solution does not use strings (including
> the terminating `\0' character).

> Better one :

> ****************************

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char * argv[])
> {
>   char Freq[6] = "AAAAA\0";

"AAAAA\0" is technically not a string, since it contains an
embedded null character. But since the above is equivalent to

char Freq[6] = "AAAAA";

...Freq will contain a string anyway, by coincidence. The reader
of the code is likely to get confused, however.

Quote:
>   char fiveletters[1][6];
>   strcpy(fiveletters[0], Freq);

>   printf("Freq=%s\nfiveletters=%s\n", Freq, fiveletters[0]);

>   return 0;
> }

> *****************************

> Last one :

> *****************************

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char * argv[])
> {
>   char Freq[6];

>   Freq[0]='A';
>   Freq[1]='A';
>   Freq[2]='A';
>   Freq[3]='A';
>   Freq[4]='A';
>   Freq[5]='\0';

>   printf("Freq=%s\n", Freq);

>   return 0;
> }

This is a long-winded (albeit correct) way of writing

#include <stdio.h>

int main(void)
{
char Freq[] = "AAAAA";

printf("Freq=%s\n", Freq);

return 0;

Quote:
}

> *********************************

> I suggest you to get some C tutorial and read carrefully the

--

Stockholm, Sweden

Wed, 06 Apr 2005 05:42:53 GMT
A beginner question - Urgent need help.

<snip>

Quote:

> > I suggest you to get some C tutorial and read carrefully the
> > chapter about the strings.

humm ... I suggest me to get some C tutorial and read carrefully the
I'm thinking only that was interesting in my post.

--
Xebax

Thu, 07 Apr 2005 03:43:21 GMT
A beginner question - Urgent need help.
Thank you for your help. ^_^
Reala

Quote:

> <snip>

> > > I suggest you to get some C tutorial and read carrefully the
> > > chapter about the strings.

> humm ... I suggest me to get some C tutorial and read carrefully the
> I'm thinking only that was interesting in my post.

> --
> Xebax

Sat, 09 Apr 2005 10:32:27 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages