This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: string handling bug or feature?


Feature, use `-Wwrite-strings' to warn about it.
Use `-fwritable-strings' to get them as writable.

Thanks,
Andrew Pinski

On Saturday, May 17, 2003, at 17:52 US/Eastern, Dirk Petry wrote:

Hi!

The following problem is present in C programs compiled with
gcc 2.95.2 and gcc 3.2.3 :

It is apparently not possible to create an array of strings
with a statement like e.g.
char *c[] = { "abcd", "cdef" };
and then modify the strings with statements like e.g.
c[0][0] = 'x';

However, when you create the array of strings using
char c[2][5] = { "abcd", "cdef" };
everything works fine.

Apparently, the two versions of the array "c" have
internally a different data type and behave differently
although they should both behave like "char**" for the
programmer.

Is this a bug or a feature?


Below are included three little programs. The first two (test.c and test2.c) give segmentation faults. The lines in which the faults arrise are marked.

The third program (test3.c) uses the statement
char c[2][5] = { "abcd", "cdef" };
to create the array of strings and runs fine.

Regards,

Dirk Petry


-- Dirk Petry - NASA/GSFC, Code 661, Greenbelt, MD 20771 mailto:petry@milkyway.gsfc.nasa.gov +1-301-286-0810

-----------
test.c:

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

typedef struct {
  char xxx[5];
  int xy;
} row;

void writerow( row * );

int main(){

row a;

  sprintf(a.xxx, "1234");
  a.xy = 1;

writerow( &a );

  return(0);
}

void writerow( row *b ){

char *c[] = { "abcde", "cdefe" };

fprintf( stdout, "%s, %s, %d\n", c[0], b->xxx, b->xy );

fprintf( stdout, "%d, %d\n", sizeof(c[0]), sizeof(b->xxx) );

sprintf( c[0], "%s", b->xxx ); /* causes segmentation fault */

fprintf( stdout, "%s, %d\n", c[0], b->xy );

return;

}

--------
test2.c:

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

typedef struct {
  char xxx[5];
  int xy;
} row;

void writerow( row * );

int main(){

row a;

  sprintf(a.xxx, "1234");
  a.xy = 1;

writerow( &a );

  return(0);
}

void writerow( row *b ){

int i;

char *c[] = { "abcde", "cdefe" };

fprintf( stdout, "%s, %s, %d\n", c[0], b->xxx, b->xy );

fprintf( stdout, "%d, %d\n", sizeof(c[0][0]), sizeof((b->xxx)[0]) );

  for(i=0; i<5; i++){
    fprintf(stdout, "%c %c\n", c[0][i], (b->xxx)[i]);
  }

  (b->xxx)[0] = 'x';
  c[0][0] = 'x'; /* causes segmentation fault */

fprintf( stdout, "%s, %s, %d\n", c[0], b->xxx, b->xy );

return;

}

------------
test3.c:

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

typedef struct {
  char xxx[5];
  int xy;
} row;

void writerow( row * );

int main(){

row a;

  sprintf(a.xxx, "1234");
  a.xy = 1;

writerow( &a );

  return(0);
}

void writerow( row *b ){

char c[2][5] = { "abcd", "cdef" };

fprintf( stdout, "%s, %s, %d\n", c[0], b->xxx, b->xy );

fprintf( stdout, "%d, %d\n", sizeof(c[0]), sizeof(b->xxx) );

  sprintf( c[0], "%s",  b->xxx ); /* OK */
  c[0][0] = 'x'; /* OK */

fprintf( stdout, "%s, %s, %d\n", c[0], b->xxx, b->xy );

return;

}







Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]