Thread: pointers to pointers

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Red face Dynamic malloc + realloc

    Can anyone please help me with my confused idea of pointers to pointers. I need to write code to input a poem from a user and output the poem backwards(line by line). I made a fairly pathetic attempt but can anyone tell me what im doing wrong!!!!!!
    P.S: I also need to use malloc and realloc

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


    int getline(char line[],int max)

    main()
    {
    int nch = 0;
    int c;
    max = max - 1;

    while((c = getchar*()) !=EOF)
    {
    if(c=='\n')
    break;

    if(nch<max)
    {
    line[nch] = c;
    nch = nch + 1;
    }
    }
    if(c==EOF&& nch ==0)
    return EOF;

    line[nch] = '\0';
    return nch;
    }


    {
    char *temp_line_ptr;
    char *line_ptr;
    char **poem_ptr;
    char **temp_poem_ptr;

    printf("Enter a line of a poem\n",nch);

    char *temp_line_ptr = nch;
    len = malloc(strlen(*temp_line_ptr)+1);
    strcpy(line_ptr,temp_line_ptr);

    line_ptr = malloc(sizeof(CHAR)*len);
    if(line_ptr == NULL)
    {
    printf("out of memory\n");
    return
    }

    poem_ptr = malloc(sizeof(char*))
    *poem_ptr = line_ptr;

    while(getline(line,CHAR) != EOF)
    {

    *line_ptr = &temp_line_ptr;
    **temp_poem_ptr = &poem_ptr;
    }

    temp_poem_ptr = realloc(poem_ptr,sizeof(char*));
    if(temp_poem_ptr !=NULL)
    {
    line_ptr = temp_poem _ptr;
    }
    else
    {
    printf("out of memory\n");
    }
    else
    {
    poem_ptr = temp_poem_ptr;
    }
    *poem_ptr = line_ptr;

    printf("\n,line_ptr)

    free(line_ptr)

    line_ptr = NULL;

    }
    Last edited by pwbtech; 02-08-2003 at 10:25 AM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: pointers to pointers

    Try adding code tags.

    Code:
    int getline(char line[],int max); //added semicolon
    Code:
    int main(void)  //added int

    Code:
    {
    char *temp_line_ptr;
    char *line_ptr;
    char **poem_ptr;
    char **temp_poem_ptr;
    I dont understand this code, this is outside the int main method and doesnt belong to any other methods ? ::confussed::


    Code:
      if(temp_poem_ptr !=NULL)
      {
        line_ptr = temp_poem _ptr;
      }
      else
      {
        printf("out of memory\n");
      }
      else
      {
      poem_ptr = temp_poem_ptr;
      }
    Two else statments ? :confussed:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM