copy char into char[] 
Author Message
 copy char into char[]

Hello,

I have the following:

char temp,theLine[1000],finalLine[1000];
temp=theLine[count];
strcpy(finalLine,temp);   //I get an error here

How do I go about doing the above?

Thanks!

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 06 May 2003 03:00:00 GMT  
 copy char into char[]
finalLine[count]=theLine[count];
or
char temp[2],theLine[1000],finalLine[1000];
temp[1]=0
temp[0]=theLine[count];
strcpy(finalLine,temp);

Vladimir Kuznetsov
Brainbench C Programming "Most Valuable Professional"

Quote:

> Hello,

> I have the following:

> char temp,theLine[1000],finalLine[1000];
> temp=theLine[count];
> strcpy(finalLine,temp);   //I get an error here

> How do I go about doing the above?

> Thanks!

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 06 May 2003 03:00:00 GMT  
 copy char into char[]
The two code snippets you recommend are not equivalent. Either the first
should read

finalLine[0] = theLine[count];
finalLine[1] = 0;

or the second should read

char temp[2],theLine[1000],finalLine[1000];
temp[1]=0
temp[0]=theLine[count];
strcpy(finalLine + count, temp);

--
With best wishes,
    Igor Tandetnik


Quote:
> finalLine[count]=theLine[count];
> or
> char temp[2],theLine[1000],finalLine[1000];
> temp[1]=0
> temp[0]=theLine[count];
> strcpy(finalLine,temp);

> Vladimir Kuznetsov
> Brainbench C Programming "Most Valuable Professional"


> > Hello,

> > I have the following:

> > char temp,theLine[1000],finalLine[1000];
> > temp=theLine[count];
> > strcpy(finalLine,temp);   file://I get an error here

> > How do I go about doing the above?

> > Thanks!

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.



Tue, 06 May 2003 03:00:00 GMT  
 copy char into char[]
That was supposed that code runs in a cycle as that most probably is. Note
the 'count' variable that is not defined in the original snippet. And, yes,
you are right the 'finalLine' in the second snippet should be incremented
somehow. However, the intent of the snippet was to just show difference
between char and string - it is far less efficient so it's hard to believe
that somebody is going to use it.
Also, probably, strcpy( finalLine, theLine ) will do.
And, if you like any weird implementations:
char temp,theLine[1000],finalLine[1000];
for(...){
  temp=theLine[count];
  memcpy(finalLine+count,&temp,sizeof temp);

Quote:
}

Vladimir Kuznetsov
Brainbench C Programming "Most Valuable Professional"


Quote:
> The two code snippets you recommend are not equivalent. Either the first
> should read

> finalLine[0] = theLine[count];
> finalLine[1] = 0;

> or the second should read

> char temp[2],theLine[1000],finalLine[1000];
> temp[1]=0
> temp[0]=theLine[count];
> strcpy(finalLine + count, temp);

> --
> With best wishes,
>     Igor Tandetnik



> > finalLine[count]=theLine[count];
> > or
> > char temp[2],theLine[1000],finalLine[1000];
> > temp[1]=0
> > temp[0]=theLine[count];
> > strcpy(finalLine,temp);

> > Vladimir Kuznetsov
> > Brainbench C Programming "Most Valuable Professional"




- Show quoted text -

Quote:
> > > Hello,

> > > I have the following:

> > > char temp,theLine[1000],finalLine[1000];
> > > temp=theLine[count];
> > > strcpy(finalLine,temp);   file://I get an error here

> > > How do I go about doing the above?

> > > Thanks!

> > > Sent via Deja.com http://www.deja.com/
> > > Before you buy.



Tue, 06 May 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. copying unsigned long into char[4] or char*

2. Help ! /* copy the file char by char */

3. Converting char array of literals chars to escape chars

4. char *c,char c[ ] and char *c[ ]

5. char, unsigned char, signed char

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

7. Convert char* to char __gc[]

8. problem assigning char* to char*

9. char** and const char** ...

10. char* -> char (newbie question)

11. char *c vs. char c[]

12. what is the difference between this types: char and System.Char

 

 
Powered by phpBB® Forum Software