Member Avatar for arcticM

I have a struct WRITER and I made

typedef WRITER* WriterPtr;

also there is another struct called BOOK and a

typedef BOOK* BookPtr

in my BOOK struct I have a

WriterPtr writerPtr;  //pointer to the writer struct

now I have a func:

void choiceA(BookPtr parr, int n,WriterPtr *pWriter, int *psize)

I did realloc to add a new writter to my dynamic array of writers like that-

WriterPtr ptr=(WriterPtr)realloc(*pWriter,(*psize+1)*sizeof(WRITER)))==NULL); //I did a check that there was enough space
*pWriter=ptr;
(*psize)++;

and I initialized the writers data but I'm having a hard time putting the new writer struct into book!! like I said in my BOOK struct I have a

WriterPtr writerPtr;  //pointer to the writer struct

can anyone please help me to put the address of the new writer into that pointer?
I tried a lot of stuff but because my func gets **pWriter (WriterPtr *pWriter) I'm having a hard time figuring out how to do it right!!:@
here's what I tried
parr.writerPtr=(*pWriter+*psize);
parr.writerPtr=pWriter[*psize];

Member Avatar for arcticM

There's nothing more to realy see..I gave out all the needed info the rest is just printf /scanf bla bla bla..
I just need help with the last line in my post..

There's nothing more to realy see..I gave out all the needed info the rest is just printf /scanf bla bla bla..
I just need help with the last line in my post..

when it comes to pointers to pointer it becomes a little difficult to asssume the thing thats why i asked for the whole program so that i can run it and see whats the problem.

thanks

I'm not a fan of typedefed pointers, I find them obfuscatory.

I did realloc to add a new writter to my dynamic array of writers like that-

WriterPtr ptr=(WriterPtr)realloc(*pWriter,(*psize+1)*sizeof(WRITER)))==NULL); //I did a check that there was enough space
*pWriter=ptr;
(*psize)++;

Are you trying to do too much in that first line? Something like this instead?

void foo(WriterPtr *pWriter, int *psize)
{
   WriterPtr ptr = realloc(*pWriter, (*psize + 1) * sizeof *ptr);
   if ( ptr )
   {
      *pWriter = ptr;
      (*psize)++;
   }
}
Member Avatar for arcticM

I did this-

if((WriterPtr ptr=(WriterPtr)realloc(*pWriter,(*psize+1)*sizeof(WRITER)))==NULL)
{
       printf("Not enough memory to allocate\n");
       return ;
}
 *pWriter=ptr;
(*psize)++;
Member Avatar for arcticM

I'm not a fan of typedefed pointers, I find them obfuscatory

Let's say I didn't use typedef and my function looks like this-

void choiceA(BOOK* parr, int n,WRITER** pWriter, int *psize)

how can I put that struct address into that pointer then?

I'm not sure if I fully understand what you're trying to do. Let's see if this "made up" code is close to what it is you want.

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

typedef struct
{
   int bar;
} WRITER;

typedef struct
{
   WRITER *writer;
} BOOK;

void choiceA(BOOK* parr, int n, WRITER** pWriter, int *psize)
{
   WRITER *ptr = realloc(*pWriter, (*psize + 1) * sizeof *ptr);
   if ( !ptr )
   {
      printf("Not enough memory to allocate\n");
      return ;
   }
   *pWriter = ptr;
   (*psize)++;
   parr->writer = ptr;
   printf("parr->writer = %p\n", (void*)parr->writer);
}

int main(void)
{
   int size = 1;
   BOOK book = {0};
   WRITER *writer = malloc(size * sizeof *writer);
   choiceA(&book, 0, &writer, &size);
   printf("book.writer = %p\n", (void*)book.writer);
   return 0;
}

/* my output
parr->writer = 003D39B8
book.writer = 003D39B8
*/

If I chose to further tinker because it's Friday, I might wander off and do this.

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

typedef struct
{
   int bar;
} WRITER;

typedef struct
{
   WRITER *writer;
   int count;
} BOOK;

/** Return NULL if attempt fails, non-NULL if successful */
void* add_writer(BOOK* book)
{
   WRITER *temp = realloc(book->writer, (book->count + 1) * sizeof *temp);
   if ( temp )
   {
      book->writer = temp;
      ++book->count;
      printf("writer = %p\n", (void*)book->writer);
      return book;
   }
   return 0;
}

int main(void)
{
   BOOK book = {0};
   add_writer(&book);
   printf("writer = %p, count = %d\n", (void*)book.writer, book.count);
   add_writer(&book);
   printf("writer = %p, count = %d\n", (void*)book.writer, book.count);
   return 0;
}

/* my output
writer = 003D39B8
writer = 003D39B8, count = 1
writer = 003D39B8
writer = 003D39B8, count = 2
*/

But then again, I'm not too sure what it is you're after. (I'm running a bit slow today in general.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.