alt.comp.lang.c
  Home FAQ Contact Sign in
alt.comp.lang.c only
 
Advanced search
April 2008
motuwethfrsasuw
 123456 14
78910111213 15
14151617181920 16
21222324252627 17
282930     18
2008
 Jan   Feb   Mar   Apr 
 May   Jun   Jul   Aug 
 Sep   Oct   Nov   Dec 
2008 2007 2006  
total
alt.comp.lang.c Profile…
RELATED GROUPS

POPULAR GROUPS

more...

 Up
  Allocating a pointer?         


Author: saneman
Date: Apr 30, 2008 06:03

In the below code the last call to myprint does not work (gives a
segmentation error).

But I thought that pointer variables local in main (or any other
function) would first be deleted when the calling function returns. Why
does it give a segmentation fault?

#include
#include

void myprint(double* a) {
printf("number is = %%f\n",*a);

}

int main() {

/* Working! (static allocation) */
double d1 = 1.1;
myprint(&d1);

/* Working! (dynamic allocation) */
double* d2 = (double*) malloc(sizeof(double*));
*d2 = 2.2;
myprint(d2);
Show full article (0.72Kb)
29 Comments
  Length of Strings in an Array at compile-time         


Author:
Date: Apr 29, 2008 05:41

How do I find the length of strings within an array at compile time?

The following is not safe:

const char* MyStrings[] =
{
"one",
"two",
"three",
"end marker"
}

#define StringLength(index) MyStrings[index+1] - MyStrings[index]

Since the strings may not be placed consecutively, or in order, within
memory.

Also

#define String1 "one"
#define String2 "two"
#define String3 "three"
Show full article (0.65Kb)
5 Comments