Deepak
Deepak

Reputation: 2053

Pointer initialization and string manipulation in C

  1. I have this function which is called about 1000 times from main(). When i initialize a pointer in this function using malloc(), seg fault occurs, possibly because i did not free() it before leaving the function. Now, I tried free()ing the pointer before returning to main, but its of no use, eventually a seg fault occurs.
    1. The above scenario being one thing, how do i initialize double pointers (**ptr) and pointer to array of pointers (*ptr[])?
    2. Is there a way to copy a string ( which is a char array) into an array of char pointers. char arr[]; (Lets say there are fifty such arrays) char *ptr_arr[50]; Now i want point each such char arr[] in *ptr_arr[] How do i initialize char *ptr_arr[] here?
    3. What are the effects of uninitialized pointers in C?
    4. Does strcpy() append the '\0' on its own or do we have to do it manually? How safe is strcpy() compared to strncpy()? Like wise with strcat() and strncat().

Thanks.

Upvotes: 1

Views: 2849

Answers (6)

Tareq
Tareq

Reputation: 11

A pointer is a special type of variable which holds the address or location of another variable. Pointers point to these locations by keeping a record of the spot at which they were stored. Pointers to variables are found by recording the address at which a variable is stored. It is always possible to find the address of a piece of storage in C using the special & operator. For instance: if location were a float type variable, it would be easy to find a pointer to it called location_ptr

float location;
float *location_ptr,*address;

location_ptr = &(location);

or

 address = &(location);

The declarations of pointers look a little strange at first. The star * symbol which stands in front of the variable name is C's way of declaring that variable to be a pointer. The four lines above make two identical pointers to a floating point variable called location, one of them is called location_ptr and the other is called address. The point is that a pointer is just a place to keep a record of the address of a variable, so they are really the same thing.

A pointer is a bundle of information that has two parts. One part is the address of the beginning of the segment of memory that holds whatever is pointed to. The other part is the type of value that the pointer points to the beginning of. This tells the computer how much of the memory after the beginning to read and how to interpret it. Thus, if the pointer is of a type int, the segment of memory returned will be four bytes long (32 bits) and be interpreted as an integer. In the case of a function, the type is the type of value that the function will return, although the address is the address of the beginning of the function executable.

Also get more tutorial on C/C++ Programming on http://www.jnucode.blogspot.com

Upvotes: 1

Tamás Szelei
Tamás Szelei

Reputation: 23971

  1. Segfault can be caused by many things. Do you check the pointer after the malloc (if it's NULL)? Step through the lines of the code to see exactly where does it happen (and ask a seperate question with more details and code)

  2. You don't seem to understand the relation of pointers and arrays in C. First, a pointer to array of pointers is defined like type*** or type**[]. In practice, only twice-indirected pointers are useful. Still, you can have something like this, just dereference the pointer enough times and do the actual memory allocation.

  3. This is messy. Should be a separate question.

  4. They most likely crash your program, BUT this is undefined, so you can't be sure. They might have the address of an already used memory "slot", so there might be a bug you don't even notice.

Upvotes: 4

Jason Williams
Jason Williams

Reputation: 57952

You've added an additional question about strcpy/strncpy.

strcpy is actually safer. It copies a nul terminated string, and it adds the nul terminator to the copy. i.e. you get an exact duplicate of the original string.

strncpy on the other hand has two distinct behaviours:

  • if the source string is fewer than 'n' characters long, it acts just as strcpy, nul terminating the copy
  • if the source string is greater than or equal to 'n' characters long, then it simply stops copying when it gets to 'n', and leaves the string unterminated. It is therefore necessary to always nul-terminate the resulting string to be sure it's still valid:
    char dest[123];
    strncpy(dest, source, 123);
    dest[122] = '\0';

Upvotes: 0

bbg
bbg

Reputation: 3080

for part 'a', maybe this helps:

void myfunction(void) {
  int * p = (int *) malloc (sizeof(int));
  free(p);
}

int main () {
  int i;
  for (i = 0; i < 1000; i++)
    myfunction();

  return 0;
}

Here's a nice introduction to pointers from Stanford.

Upvotes: 1

Andrew Song
Andrew Song

Reputation: 3128

It's hard to answer your first question without seeing some code -- Segmentation Faults are tricky to track down and seeing the code would be more straightforward.

Double pointers are not more special than single pointers as the concepts behind them are the same. For example...

char * c = malloc(4);
char **c = &c;

I'm not quite sure what c) is asking, but to answer your last question, uninitialized pointers have undefined action in C, ie. you shouldn't rely on any specific result happening.

EDIT: You seem to have added a question since I replied...

strcpy(..) will indeed copy the null terminator of the source string to the destination string.

Upvotes: 1

Jason Williams
Jason Williams

Reputation: 57952

From your question, my advice would be to google "pointers in C" and read some tutorials to get an understanding of what pointers are and how to use them - there's a lot that would need to be repeated in an SO answer to get you up to speed.

The top two hits are here and here.

Upvotes: 1

Related Questions