Quote Originally Posted by itsme86
Your problem is here. Firstly, scrap the first allocation, just initialize ptr to NULL. Next, you're only allocating memory for the current arg in your realloc() call.
Right, so i have changed the code to this....
Code:
/* test.c - test program to see how realloc works   *
 * requires: at least 1 arg, of any type, or length */

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

int main(int argc, char **argv){

  char *ptr;       /* pointer to allocated memory */

  /* make sure there are args */
  if (argc == 0)
    return 1;

  ptr = NULL;

  /* put args into memory, putting a '\n' between each arg */
  for (i = 0; i < argc; i++){
    /* allocate new memory to hold the arg */
    ptr = realloc(ptr, (ptr ? strlen(ptr) : 0) + strlen(argv[i]) + 2);
    /* occupy the allocated memory with arg data */
    sprintf(ptr + strlen(ptr), "%s\n", argv[i]);
  }

  /* PRINT DATA FROM MEMORY */
  
  /* finished! */
  return 0;
  
} /* main */
However, how do i record how many chars have been read so i can print whatever is in the allocated memory???