🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #52291

?Strange behavior of char* vs. char[]

#52291
From: Morgan Stair
Date: Thu, 10 Dec 1992 23:48
69 lines
3516 bytes
I'm working on:
    a Sun SPARCstation 1+ / SunOS 4.1.1 / gcc-2.3.2 / gdb-4.3 and
    and a DECstation 5000/200 / Ultsux 4.2 / gcc-2.3.1 / gdb-4.3

When I compile the following (2 file) program all indications from gdb
are that everything should be fine, but without fail I get
segmentation faults.  I didn't want to post this to BUGS because
changing the "extern char *x" of sub.c to "extern char x[]" gets rid
of the errror, but I can't see why this happens.

Any explanations???  This was done on a DECstation, but the results
are identical on the Sun, except that it seg-faults in _doprnt ()
instead of strlen ().

Here's the gcc command line (that returns happily with no errors:
    gcc -Wall -g -o main main.c sub.c

Here's the two files side by side:
    /*** main.c ***/                     |/*** sub.c ***/
    #include <stdio.h>                   |#include <stdio.h>
    extern void sub (void);              |extern char *x;
    char x[100];                         |char path[100];
                                         |
    int main () {                        |void sub () {
      sprintf (x, "Hi there");           |  sprintf (path, "%s", x);
      sub ();                            |  printf ("%s\n", path);
      return 0;                          |}
    }                                    |

And here's some confusing gdb output for you:
    (gdb) run
    Starting program: /home/morgan/sun4/Wall/tmp/main

    Program received signal 11, Segmentation fault
    strlen () at ../strlen.c:36
    ../strlen.c:36: No such file or directory.
    (gdb) bt
    #0  strlen () at ../strlen.c:36
    #1  0x4018a0 in _doprnt () at ../doprnt.c:918
    #2  0x400558 in sprintf () at ../sprintf.c:102
    #3  0x400228 in sub () at sub.c:6
    #4  0x4001e0 in main () at main.c:7
    (gdb) frame 3
    #3  0x400228 in sub () at sub.c:6
    (gdb) l
    1       #include <stdio.h>
    2       extern char *x;
    3       char path[100];
    4
    5       void sub () {
    6         sprintf (path, "%s", x);
    7         printf ("%s\n", path);
    8       }
    (gdb) p x
    $1 = {"Hi there", '\000' <repeats 92 times>}
    (gdb)

Like I said, I don't think this is a bug, since declaring x the same
way in both files makes it work right, but I thought arrays and
pointers were pretty much the same thing???
--
*************************************************************************
* W. Morgan Stair                        Phone: (617) 552-8783          *
* Boston College                         FAX:   (617) 552-8778          *
* Institute for Space Research           Email: morgan@dl5000.bc.edu    *
* 4 Alfred Circle                                                       *
* Bedford MA  01730                                                     *
*************************************************************************
Technical Details
Message-ID: <MORGAN.92Dec10184806@dl5000.bc.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!hsdndev!news.cs.umb.edu!morgan

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,de044988a43db4e8,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1992-12-10 16:14:37 PST
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!hsdndev!news.cs.umb.edu!morgan
From: morgan@dl5000.bc.edu (Morgan Stair)
Newsgroups: gnu.gcc.help
Subject: ?Strange behavior of char* vs. char[]
Message-ID: <MORGAN.92Dec10184806@dl5000.bc.edu>
Date: 10 Dec 92 23:48:06 GMT
Sender: news@cs.umb.edu (USENET News System)
Distribution: usa
Organization: Institute for Space Research, Boston College
Lines: 69
Nntp-Posting-Host: dl5000.bc.edu