Re: [Code] general

From: Ron Hensley (ron@cross.dmv.com)
Date: 01/27/97


> Hello, I never could figure out why when you declare a variable like that
> char *message;

char *message;

decalare a variable of type:
  pointer to a character.
It points nowhere. What it is, is a variable that stores the memory 
location where a 'character' is stored.

They are used just like arrays most times, that being when they point to 
a spot in memory where there is a list of characters (byte sized momory 
locations), stored in linear fasion.

In C, when you use %s in a printf statement, you give as an agument, 
either a pointer itself, as youve done down there, using the name of a 
pointer variable, or you could use the address of an actual character 
variable.

I.E.
  char achar[10];
  achar[0] = 'a';
  acahar[1] = 'b';
  achar[2] = '\0';

  printf("A string: %s\n", &achar[0]);

OR:
  char *aptrtoachar;

  aptrtoachar = (char *)malloc(10);

  strcpy(aptrtoachar, "ab");

  printf("A string: %s\n", aptrtoachar);

Anyways, when aptrtoachar is declared, its just a pointer pointing absolutely
nowhere. its junk. Printf assumes its supposed to go the address stored 
in the variable and start printing characters till it finds a NULL ( \0  )

It points god knows where and starts reading garbage and you get a seg fault.

You choices are:
  use malloc if you know how big of an area you need, but not recessarily 
    whats to go in there, as buf does that you always see.
  use an array, much simpler if you arent familiar with pointers.

  char myvariable[20];

  If you now whats to go in there from the gitgo:

  char *aptrtoachar = "The string i want to store here";

When you declare it this way the compiler sets aside room to store the 
string and makes the variable point to it.


Anyways im no C teacher, im sure ill be flamed to hell and back, and told 
i know nothing at all for even trying to help.

Just get a nice book on C programming, a good cup of coffee, a comfy 
chair, relax and read up

> and you use this variable in a printf you get a segfault
> printf("this is the value of message: %s\r\n",message);
> It has nothing to do with the mud but maybe someone knows.
> thanks
> Mehdi Keddache (Heritsun on Eclipse of Fate eclipse.argy.com 7777)


    *******************************************************************
    *   Ron Hensley                     ron@dmv.com                   *
    *   Systems Administrator           http://www.dmv.com/~ron       *
    *                                   PGP Key at WWW Page           *
    *   DelMarVa OnLine                 749-7898 Ext. 403             *
    *******************************************************************

+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST