[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] initalisering i c



In article <sslug@sslug> you wrote:

> Håber der er en der kan hjælpe mig med et umiddelbart banalt spørgsmål!
> Hvordan initialisere man et char array i c ??
> Char line[50]  så det bliver en helt tom streng??

Måske :
  char line[50] = "";
Eller
  char line[50]= "           tæl 50 blanktegn                      ";

> Den bliver brugt til at modtage en kommando via læs fra socket

> Char line [50];
> Char c;

Char er det ikke med lille: "char" ?

> while ( ch != '\n')
> {
> read (fd, &ch, 1);
> line[y] = ch;
> y++;
> }


Uha hvad med overløb hvis du får en meget lang linie ?
Der bør være en test for max. størrelse af input linie.
Og egentlig kan det kodes så du ikke behøver at initialisere hele strengen.
Du glemmer egentlig blot at afslutte med "0".

enum { MaxLine=50};
char line[MaxLine];
char c;

y=0;
do {
 read (fd, &ch, 1);
 line[y] = ch;
 y++;
} while ( ch != '\n' && y<MaxLine-1) {
line[y]= '\0';

Og egentlig bør du teste at read() overhovedet læste 1 tegn
og ikke gav fejl eller at forbindelsen er afsluttet.



-- 
Frank Damgaard 



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *