f



pointer vs pointer to pointer

AIM: To write a queue with 3 operations.
WHAT I GOT: It works
PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a 
pointer" to deleteElement() both work fine ? Will enqueue() work fine if 
I pass just "a pointer" to it ?  Will deleteElement() work fine if I pass 
"pointer to pointer" ?

/* A queue implementataion with the operations that I need:
 *  (1) Add element to the front of the queue
 *  (2) remove an element with a unique ID (string constant)
 *  (3) compare 2 elements (string comparison) 
 *  (4) print queue
 *
 * VERSION 0.1
 */

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

enum { SIZE_ID = 100 };

struct myQ
{
  char id[SIZE_ID+1];
  struct myQ* next;
};

struct myList
{
  struct myQ* head;
};



int enqueue(struct myList** s, const char* id);
void deleteElement(struct myList* s, const char* id);
int compareElements(struct myQ* e1, struct myQ* e2);
void printQ(struct myList* s);



int main(void)
{
  struct myList* s = malloc(1 * (sizeof *s));
  if(NULL == s) exit(EXIT_FAILURE);

  enqueue(&s, "1");
  enqueue(&s, "2");enqueue(&s, "3"); 
  printQ(s);
  deleteElement(s, "3");
  printQ(s);
  deleteElement(s, "2");
  printQ(s);
  deleteElement(s, "1");
  printQ(s);

  free(s);
  return 0;
}


int enqueue(struct myList** s, const char* id)
{
  struct myQ *p, *h;
  if(NULL == s || NULL == id) return -1;
  p = malloc(1 * (sizeof *p));
  if(NULL == p) return -1;
  strcpy(p->id, id);
  p->next = NULL;
  
  h = (*s)->head;
  (*s)->head = p;
  p->next = h;
  
  return 1;
}


void deleteElement(struct myList* s, const char* id)
{
  struct myQ *prev, *curr;
  if(NULL == s || NULL == id) return;
  prev = s->head;
  for(curr = s->head; curr; curr = curr->next)
    {
      if(0 == strcmp(curr->id, id))
	{
	  prev->next = curr->next;
	  if(0 == strcmp(s->head->id, curr->id)) s->head = curr->next;
	  free(curr);
	  break;
	}
      else
	{
	  prev = curr;
	}
    }
}


void printQ(struct myList* s)
{
  if(s)
    {
      struct myQ* t;
      for(t = s->head; t; t = t->next) printf("ID = %s\n", t->id);
    }
  else
    {
      printf("Nothing to print\n");
    }

  printf("--------------------------------\n");
}


====================== OUTPUT ========================
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra queue.c
/home/arnuld/programs/C $ ./a.out 
ID = 3
ID = 2
ID = 1
--------------------------------
ID = 2
ID = 1
--------------------------------
ID = 1
--------------------------------
--------------------------------
/home/arnuld/programs/C $ 


-- 
 arnuld
http://LispMachine.Wordpress.com
0
sunrise2 (603)
6/11/2012 7:13:05 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

4 Replies
944 Views

Similar Articles

[PageSpeed] 59

On 11 Jun 2012 07:13:05 GMT, arnuld <sunrise@invalid.address> wrote:

>AIM: To write a queue with 3 operations.
>WHAT I GOT: It works
>PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a 
>pointer" to deleteElement() both work fine ? Will enqueue() work fine if 
>I pass just "a pointer" to it ?  Will deleteElement() work fine if I pass 

Since enqueue() uses *s ONLY as the left operand of ->, main() could
call enqueue() with s instead of &s and enqueue() could use s instead
of *s.  This would "improve" enqueue (both performance and
readability) and is worth doing.

>"pointer to pointer" ?

Similarly, deleteElement() uses s only as the left operand of -> so
main() could call deleteElement() with &s and deleteElement could use
*s in place of s.  This would detract from deleteElement (both
performance and readability) and is worth not doing.

If you want a function to update an object directly, you can pass a
pointer to that object and the function can dereference the pointer to
update the object.  If the object to be updated happens to be a
pointer, then the pointer argument in question just happens to be a
pointer to pointer.

Since neither of your functions want to update the pointer (they only
update the structure eventually pointed to), there is no need to pass
the address of the pointer.

>
>/* A queue implementataion with the operations that I need:
> *  (1) Add element to the front of the queue
> *  (2) remove an element with a unique ID (string constant)
> *  (3) compare 2 elements (string comparison) 
> *  (4) print queue
> *
> * VERSION 0.1
> */
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>enum { SIZE_ID = 100 };
>
>struct myQ
>{
>  char id[SIZE_ID+1];
>  struct myQ* next;
>};
>
>struct myList
>{
>  struct myQ* head;
>};
>
>
>
>int enqueue(struct myList** s, const char* id);
>void deleteElement(struct myList* s, const char* id);
>int compareElements(struct myQ* e1, struct myQ* e2);
>void printQ(struct myList* s);
>
>
>
>int main(void)
>{
>  struct myList* s = malloc(1 * (sizeof *s));
>  if(NULL == s) exit(EXIT_FAILURE);
>
>  enqueue(&s, "1");
>  enqueue(&s, "2");enqueue(&s, "3"); 
>  printQ(s);
>  deleteElement(s, "3");
>  printQ(s);
>  deleteElement(s, "2");
>  printQ(s);
>  deleteElement(s, "1");
>  printQ(s);
>
>  free(s);
>  return 0;
>}
>
>
>int enqueue(struct myList** s, const char* id)
>{
>  struct myQ *p, *h;
>  if(NULL == s || NULL == id) return -1;
>  p = malloc(1 * (sizeof *p));
>  if(NULL == p) return -1;
>  strcpy(p->id, id);
>  p->next = NULL;
>  
>  h = (*s)->head;
>  (*s)->head = p;
>  p->next = h;
>  
>  return 1;
>}
>
>
>void deleteElement(struct myList* s, const char* id)
>{
>  struct myQ *prev, *curr;
>  if(NULL == s || NULL == id) return;
>  prev = s->head;
>  for(curr = s->head; curr; curr = curr->next)
>    {
>      if(0 == strcmp(curr->id, id))
>	{
>	  prev->next = curr->next;
>	  if(0 == strcmp(s->head->id, curr->id)) s->head = curr->next;
>	  free(curr);
>	  break;
>	}
>      else
>	{
>	  prev = curr;
>	}
>    }
>}
>
>
>void printQ(struct myList* s)
>{
>  if(s)
>    {
>      struct myQ* t;
>      for(t = s->head; t; t = t->next) printf("ID = %s\n", t->id);
>    }
>  else
>    {
>      printf("Nothing to print\n");
>    }
>
>  printf("--------------------------------\n");
>}

-- 
Remove del for email
0
schwarzb3978 (1424)
6/11/2012 8:21:25 AM
On 2012-06-11, arnuld <sunrise@invalid.address> wrote:
> AIM: To write a queue with 3 operations.
> WHAT I GOT: It works

Sorry, there is a problem.

> int main(void)
> {
>   struct myList* s = malloc(1 * (sizeof *s));

Here, you allocate enough space for a struct Mylist.
The space is uninitialized, in particular s->head
contains garbage.

>   enqueue(&s, "1");

Looking at enqueue:

> int enqueue(struct myList** s, const char* id)
> {
>   struct myQ *p, *h;
>   if(NULL == s || NULL == id) return -1;

You might also want to bail out if *s == NULL .

>   p = malloc(1 * (sizeof *p));
>   if(NULL == p) return -1;
>   strcpy(p->id, id);
>   p->next = NULL;

This assignment is useless, as p->next is re-assigned
another value a few lines below.

>   
>   h = (*s)->head;

where (*s)->head contains garbage.

>   (*s)->head = p;
>   p->next = h;

The list that starts at (*s)->head is not
properly terminated (it's supposed to be terminated
by a null pointer, but in fact it's terminated by
garbage). That may lead to problems when walking through
the list, which happens e.g. in the for loops in printQ()
or deleteElement().

To fix this, set s->head to NULL after allocating the
space for struct myList *s in main().

Or it might be a better idea to write a constructor
function that returns a properly initialized empty list,
and use the constructor instead of using a bare malloc call.
0
ike8 (164)
6/11/2012 8:51:09 AM
On 2012-06-11, arnuld <sunrise@invalid.address> wrote:
> PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a 
> pointer" to deleteElement() both work fine ? Will enqueue() work fine if 
> I pass just "a pointer" to it ?  Will deleteElement() work fine if I pass 
> "pointer to pointer" ?

Yes and yes. Looking at enqueue:

> int enqueue(struct myList** s, const char* id)
> {
>   struct myQ *p, *h;
>   if(NULL == s || NULL == id) return -1;
>   p = malloc(1 * (sizeof *p));
>   if(NULL == p) return -1;
>   strcpy(p->id, id);
>   p->next = NULL;
>   
>   h = (*s)->head;
>   (*s)->head = p;
>   p->next = h;
>   
>   return 1;
> }

in enqueue, s is never used (only *s is),
and *s is never modified, so you might as well
substitute s for *s everywhere in this function.

>   p->next = NULL;
>   h = (*s)->head;
>   (*s)->head = p;
>   p->next = h;

can be written more simply as

    p->next = (*s)->head;
    (*s)->head = p;

eliminating h.

Combining all this, enqueue would look somewhat like

  int enqueue(struct myList* s, const char* id)
  {
    struct myQ *p;
    if (NULL == s || NULL == id) return -1;
    p = malloc(sizeof *p);
    if (NULL == p) return -1;
    strcpy(p->id, id);
    p->next = s->head;
    s->head = p;
    return 1;
  }
0
ike8 (164)
6/11/2012 9:05:47 AM
arnuld <sunrise@invalid.address> writes:

A couple of things not yet said...

> AIM: To write a queue with 3 operations.
> WHAT I GOT: It works
> PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a 
> pointer" to deleteElement() both work fine ? Will enqueue() work fine if 
> I pass just "a pointer" to it ?  Will deleteElement() work fine if I pass 
> "pointer to pointer" ?
>
> /* A queue implementataion with the operations that I need:
>  *  (1) Add element to the front of the queue
>  *  (2) remove an element with a unique ID (string constant)
>  *  (3) compare 2 elements (string comparison) 
>  *  (4) print queue
>  *
>  * VERSION 0.1
>  */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> enum { SIZE_ID = 100 };

I like to distinguish between length and size for strings.  The size is
at least one move than the length.  Since you allocate space for
SIZE_ID+1 bytes, SIZE_ID is really the maximum length, not the maximum
size.  Small point, but I think these things really help with +1 and -1
errors.

> struct myQ
> {
>   char id[SIZE_ID+1];
>   struct myQ* next;
> };
>
> struct myList
> {
>   struct myQ* head;
> };

To my mind, these two structs are the wrong way round.  struct myQ is a
list node, and struct myList represents a queue!

> int enqueue(struct myList** s, const char* id);
> void deleteElement(struct myList* s, const char* id);
> int compareElements(struct myQ* e1, struct myQ* e2);
> void printQ(struct myList* s);
>
>
>
> int main(void)
> {
>   struct myList* s = malloc(1 * (sizeof *s));
>   if(NULL == s) exit(EXIT_FAILURE);

Why not 

  struct myList s = {0};

?  It's much simpler, especially for testing and initialisation is easy.

By the way, if you possibly can, try using valgrind.  It is, to my mind,
one of the best tools a beginner can use.

<snip>
> }
>
<snip>
> void deleteElement(struct myList* s, const char* id)
> {
>   struct myQ *prev, *curr;
>   if(NULL == s || NULL == id) return;
>   prev = s->head;
>   for(curr = s->head; curr; curr = curr->next)
>     {
>       if(0 == strcmp(curr->id, id))
> 	{
> 	  prev->next = curr->next;
> 	  if(0 == strcmp(s->head->id, curr->id)) s->head = curr->next;
> 	  free(curr);
> 	  break;
> 	}
>       else
> 	{
> 	  prev = curr;
> 	}
>     }
> }

Adding a test for strcmp(s->head->id, curr->id) just to see if this is
the head of the queue is yucky!  Much simpler would be cur == s->head.

However, you can this code so that the head element is not special.  The
only reason you have 'prev', is that you can set its 'next' member to
the tail of the current location (curr->next).  I do this be keeping a
pointer to where this new list tail should be written:

  struct myQ **tail_loc = &s->head;
  for (curr = s->head; curr; curr = curr->next)
    {
      if (0 == strcmp(curr->id, id))
	{
          *tail_loc = curr->next;
	  free(curr);
	  break;
	}
      tail_loc = &curr->next;
    }

You can avoid 'curr' and using break altogether:

  struct myQ **tail_loc = &s->head;
  while (*tail_loc && strcmp((*tail_loc)->id, id))
       tail_loc = &(*tail_loc)->next;
  if (*tail_loc) {
       struct myQ *np = *tail_loc;
       *tail_loc = np->next;
       free(np);
  }

but that's maybe too fiddly.  It's much nicer in a language with
references because you loose all those *s.

<snip>
-- 
Ben.
0
ben.usenet (6790)
6/11/2012 11:44:19 AM
Reply:

Similar Artilces:

pointers, pointers, pointers...
Hi There, I came up with following sample code to demonstrate my issue: #include <18F87K22.h> #device HIGH_INTS=TRUE, adc=16, ICD=TRUE #fuses NOWDT //No Watch Dog Timer #fuses WDT128 //Watch Dog Timer uses 1:128 #fuses HSM //Hi-Speed crystal oscillator #fuses NOBROWNOUT //No brownout reset #fuses NOPLLEN //No PLL enabled #fuses BBSIZ1K //1K words Boot Block size #fuses NOXINST //Extended set extension and Indexed typedef struct{ int8 foo, test; } TheStruct; TheStruct mystruct[5]; int16 myarr[2]; void Func(TheStruct *data, int16 *arr); void main (void) { myarr[0]=0xff; myarr[1]=0xaa; memset(&mystruct,0,sizeof(mystruct)); Func(myarr,&mystruct); while(true); } //------------------------------------------------------------------------------ void Func(TheStruct *strdat, int16 *arr) { strdat[0]->foo=(*arr[0]); strdat[0]->test=(*arr[1]); } This compiles just fine (CCS 4.119) but doesn't work, after the function, foo and test are still 0 cause myarr doesn't seem to be passed correctly to the function.... what am I doing wrong here? :( Thanks, Ron In <6549b0ed-900d-4279-b737-d8fa66831d94@r3g2000yqh.googlegroups.com> cerr <ron.eggler@gmail.com> writes: > TheStruct mystruct[5]; > int16 myarr[2]; > void Func(TheStruct *data, int16 *arr); > Func(myarr,&mystruct); Func is defined to...

Pointer to pointer Vs References to Pointer
Q . When to use use pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function ? According to me Which is better : double pointers OR Reference to Pointer ? There is no clear advantage and depends on programmer =91s choice or habit . As Nilesh mentioned =94 Reference to pointer=94 makes code easy But Pointer to pointer makes code easy to understand. So one can use any one . Why Pointer to Pointer is generally used ? 1. Pointer to pointer can be used in C and C++ but reference to pointer is limited to C++. a. Generally people start learning programming from C . So they get used to pointer to pointer. b.Pointer to Pointer usage makes project porting in C or C++ easy. 2. Generally libraries uses pointer to pointer approach like COM uses pointer to pointer for CoCreateInstance or QueryInterface. Fourm : Could you please let me know if these are the only reasons or i am missing something? Thanks Vikrant Example #include "stdafx.h" void Ref2Poi(int * &p) { *p =3D 10; } void Poi2Poi( int ** p) { **p =3D 10; } int _tmain(int argc, _TCHAR* argv[]) { int i =3D 13; int j =3D 14; int *p =3D &i; int *q =3D &j; Ref2Poi(p); Poi2Poi(&q); return 0; } "bansalvikrant@gmail.com" <bansalvikrant@gmail.com> writes: > Q . When to use use pointer-to-pointer and reference-to-pointer to > modify a pointer passed to a function ? &g...

pointer to an array vs pointer to pointer
Consider the following program x.c: #include <stdlib.h> #include <stdio.h> int main() { int x[10]; int **pp = &x; pp = NULL; // just to get rid of compiler warning. return EXIT_SUCCESS; } When I compile this program under gcc 3.4.3 as gcc -std=c99 -pedantic -Wall -Wextra x.c it merely generates the warning x.c: In function `main': x.c:7: warning: initialization from incompatible pointer type I thought the line int **pp = &x; is in error because of different pointer types involved in the assignment - '&x' is a pointer to an array and 'pp' is a pointer to pointer. So I do not understand why the compiler did not generate error for this line. Is this behaviour specific to the particular gcc implementation or the ISO C99 Standard itself does not treat the above assignment as an error ? Please clarify. Thanks V.Subramanian On 09/20/2011 08:14 AM, subramanian100in@yahoo.com, India wrote: > Consider the following program x.c: > > #include <stdlib.h> > #include <stdio.h> > > int main() > { > int x[10]; > int **pp = &x; > > pp = NULL; // just to get rid of compiler warning. > > return EXIT_SUCCESS; > } > > When I compile this program under gcc 3.4.3 as > gcc -std=c99 -pedantic -Wall -Wextra x.c > > it merely generates the warning > x.c: In function `main': > x.c:7: warning: ...

Pointer-to-pointer-to-pointer question
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I finally got it working. Now here's the problem: I am able to get the 2D array dynamically allocated correctly as long as I am doing it "in-line" (i.e. without invoking any function). The moment I try to do it in another function, I get a core dump. Any help will be appreciated. Since this function is expected to update a pointer-to-pointer type, I am actually passing the pointer-to-pointer-to-pointer type to the function. What am I missing here? You can see that the source code works correctly when I am perform 2D array initialization "in-line" (i.e. by not invoking a function call) simply by un-commenting the line /* #define INLINE_INIT */ Masood /******************************************************/ #include <stdio.h> #include <stdlib.h> /*#define INLINE_INIT*/ #define MAXROWS 3 #define MAXCOLS 5 void buildTbl(int ***tblPtr, size_t numRows, size_t numCols) { *tblPtr = (int **)malloc(numRows*sizeof(int*)); /* C++ : *tblPtr = new (int*)[numRows]; */ for(size_t i = 0; i < numCols; i++) *tblPtr[i] = (int *)malloc(numCols*sizeof(int)); /* C++: *tblPtr[i] = new (int)[numCols]; */ } main() { int startVal = 5; int **tbl; #ifdef INLINE_INIT tbl = (int **)malloc(MAXROWS*sizeof(int*)); /* C++ : tbl = new (int*)[MAXROWS]; */ ...

Pointers for these pointers...?
Hi Everyone: I'm trying to develop a property list to include as metadata about my object classes. i.e. I want each class I'm developing to include a PropertyList which will contain ObjectProperty pointers. ObjectProperty is a class containing the typeid.name() of a type as it's ObjectType as well as an ObjectName and ObjectDescription. Basically 3 strings of metadata describing each of a class' member variables (and hopefully functions) so that the class can report to a query from another class what properties it has available for access at run time. Here is what I have and t...

pointer to pointer
I have only used pointer to a pointer while manipulating arrays of arrays. I have not seen any C++ libraries that use pointer to a pointer as a function parameter. But I have seen it used in COM. If we want the function to create a new object and allow the passed in pointer to point to the object created we can pass a pointer to the reference. Is there anything special about COM that it needs pointer to a pointer. Is this becuase that COM can be used from C as well and we don't have references in C. In what other circumstances pointer to pointer can be used. Why don't we never see poi...

Pointer to Pointer or not?
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded my remove function as follows template <class T> bool adsQ<T>::Remove(T* theData) { Qnode<T>* removeNode = 0; if (true == isEmpty()) { //cout << "False" << endl; return false; } else { /* What I am trying to achive here is to change the data pointed to by theData to the data pointed to by pData.*/ *theData =*(pHead->pData); //Data exchan...

pointers to pointers
Hi, i understood what pointers are and all stuff, and now i'm at the part with pointers to pointers and my book doesn't really help on this issue. let be: int a,*p,**q; how to make use of **q,*q and q ? Which who's address and/or who's value contains. I would like some examples like p=&a;// p contains the address of a and *p contains it's value but regarding **q,*q respectively q. Thanks. apropo wrote: > Hi, > i understood what pointers are and all stuff, and now i'm at the part > with pointers to pointers and my book doesn't really help on...

Pointers to pointers
How can you use a "pointer to a pointer" as a two dimensional array using 'new' and 'delete' rather than 'malloc' and 'free'? Thanks, Jonas Jonas Cord wrote: > How can you use a "pointer to a pointer" as a two dimensional array > using 'new' and 'delete' rather than 'malloc' and 'free'? Pretty much the same. int main() { int** data; data = new int*[100]; for (int i = 0; i < 100; ++i) data[i] = new int[100]; //use it for (int i = 0; i < 100; ++i) delete []...

pointer of pointer
Hi I have a structure, B_t is another structure typedef struct{ int len; unsigned char code[12]; B_t *b; }A_t Now I need to pass A_t into a function to evaluate I use pointer of pointer void evaluate(A_t **a) My question is how to evaluate the component of this structure (*a)->b??? seems not right Thanks a lot! <qianz99@gmail.com> wrote in message news:1178915812.306611.242380@y80g2000hsf.googlegroups.com... > Hi > > I have a structure, B_t is another structure > > typedef struct{ > int len; > unsigned char code[12]; > B_t *b; > }A_t > > Now I need to pass A_t into a function to evaluate > I use pointer of pointer > > void evaluate(A_t **a) > > My question is how to evaluate the component of this structure > > (*a)->b??? > > seems not right > It is right. It is ugly and so you might be better off with A_t *aptr; aptr = *a; aptr->b; Code that a machine can read correctly isn't necessarily easy for humans to read. The extra variable will almost certainly be optimised away by the compiler, so there is unlikely to be any efficiency penalty. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm On May 11, 1:36 pm, qian...@gmail.com wrote: > My question is how to evaluate the component of this structure > > (*a)->b??? > It accesses member b of A_t, which a pointer to B_t. Whether it is right or not depends upon what you mean by 'eva...

pointer to a pointer
My situation: I have a function that takes a pointer to an FSref as an argument. In the body of the function is a function call that also takes a pointer to an FSRef. I need to pass my original FSRef, but since I have only a pointer to it, I am assuming I need to dereference it twice when I call the second function. However, using something like &&myFSRef in the second function doesn't work. Can anyone tell me the proper way to do this? Thanks. In article <1105802448.cd52b3255e6c95476ce23f7be215a6a3@teranews>, Steven Daedelus <sdaedelus@ira.gov> w...

pointer to pointer
Its just a very simple and basic program to learn exactly what is described in comments here. Can you provide a better (and short) exercise ? /* A program to learn how to use a pointer to a pointer. Here we take an array of char pointers. A char* in the array, of course, points * to some char. Array has only one element. We change the value of char pointed by array's element using a function. * * VERSION 0.0 */ #include <stdio.h> void replace_char_value(char* [], char); int main(void) { char* arrc[2] = {0}; char c = 'c'; arrc[0] = &c; printf("*arrc: %p, **arrc: %c\n", (void*)*arrc, **arrc); replace_char_value(arrc, 'w'); printf("*arrc: %p, **arrc: %c\n", (void*)*arrc, **arrc); return 0; } void replace_char_value(char* arr[], const char t) { **arr = t; } ==================== OUTPUT ======================= [arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra pointer-to- pointer.c [arnuld@dune programs]$ ./a.out *arrc: 0xbfffe8ab, **arrc: c *arrc: 0xbfffe8ab, **arrc: w [arnuld@dune programs]$ -- http://www.lispmachine.wordpress.com On 12/22/10 04:43 PM, arnuld wrote: > Its just a very simple and basic program to learn exactly what is > described in comments here. Can you provide a better (and short) > exercise ? Parse the arguments to a program. Don't forget argv is a pointer to pointer! Given ./a.out -a 10 -b ...

c++: pointers to pointers
Hi, I have an array of pointers defined as such: myArray** ptr = new myArray[10]; for some reason i get a compile time error stating there is no default constructor for myArray. The problem is that i should not need to define one since myArray is a pointer to a pointer and so should not have a default constructor - its a primitive data type and not an object. Any help appreciated. Regards, A A wrote in news:3f9e5e27_1@news.iprimus.com.au: > Hi, > > I have an array of pointers defined as such: > > myArray** ptr = new myArray[10]; myArray** ptr = new myArray *[10]; &g...

Once again pointer to first element vs pointer to array cast to pointer to element
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a[10][10]) { int *b=(void*)a; int *c=a[0]; /*...*/ } Now the questions come: - Is it guaranteed that b and c points to the same object (or is this implementation defined)? - Is it always true that a[1][1] refers to the same object as c[11] ? (There is no hole) - Does dereferencing b violate any aliasing rule? Thanks Szabolcs Szabolcs Borsanyi wrote: > I know that this topic has been discussed a lot, s...

pointer to an array of pointers
Hi, Could anyone please tell me how to dereference a pointer to an array of pointers? Regards On Dec 3, 7:13 pm, ramu <ramu....@gmail.com> wrote: > Hi, > Could anyone please tell me how to dereference a pointer to an > array of pointers? > > Regards *p or p[0]. A pointer to an array of N pointers is char *(*)[N]. Example: char *(*foo)[N] = NULL; "ramu" <ramu.ask@gmail.com> wrote in message news:f0977e82-d97d-4124-8f94-f3addd5b6c8a@s8g2000prg.googlegroups.com... > Hi, > Could anyone please tell me how to dereference a pointer to ...

pointers to pointers question
How come something like: #include <stdio.h> int main(int argc, char **argv) { char **arg; int i; for ( i = 0; argv[i] != NULL; ++i ) { puts(argv[i]); } for ( arg = argv; *arg != NULL; ++arg ) { puts(*arg); } return 0; } doesn't require a explicit & before argv in the for loop, but something like #include <stdio.h> #include <stdlib.h> int main(void) { char *ptr = "Garbage"; char **pptr; pptr = &ptr; printf("The value is: % s\n", *pptr); } does require an explict & Chad Chad w...

Pointer to a Pointer to a struct
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { typedef struct { char **test; }Testing; Testing **p; p = malloc(sizeof(Testing)); p[0]->test[0] = malloc(sizeof(Testing)); strncpy(p[0]->test[0],"Hello World",20); printf("%s\n",p[0]->test[0]); return EXIT_SUCCESS; } Why does the following crash ? Not sure what i'm missing please advise thanks. -- Unix Systems Engineer The City of New York Dept. of Information Technology http://www.nyc.gov/doitt rbrown[(@)]doitt.nyc.gov http://www.rodrickb...

Confused about pointer on pointer
Hi all, i've a problem with a simple example program: i've a variable: float *vertices; and want a function: initVertices(float **v, int l) which should init the vertices array. It should be filled with values from 0 to l (l should also have the size l) initVertices(float **v, int l){ // ? // ? } int main(int argc, char **argcv){ int l = 10; float *vertices=NULL; initVertices(&vertices, l); //should output 0.0, 1.0, 2.0, ... for(int i=0; i<l; i++) printf("%i, &quo...

using pointer to pointer(**)
Hello! I have noticed that if you want to return an object from a method the example I have seen always use **. Can somebody explain why? //Tony "Tony Johansson" <johansson.andersson@telia.com> kirjutas: > Hello! > > I have noticed that if you want to return an object from a method the > example > I have seen always use **. > > Can somebody explain why? > Seems like C code or unjustified premature optimization attempts. In C++ the simplest way to return an object from a function would be: class A {/*...*/}; A f() { return A(); } in...

pointer-to-pointer to char
The following code can be compiled. But When I run it, it causes "Segmentation fault". int main(){ char **c1; *c1 = "HOW"; // LINE1 ++(*c1); *c1 = "ARE"; ++(*c1); *c1 = "YOU"; } c1 is a pointer-to-pointer to char. How to initialize c1 to three strings: "HOW", "ARE", "YOU"? Thanks a lot. Jack wrote: > The following code can be compiled. But When I run it, it causes > "Segmentation fault". > > int main(){ > char **c1; > > *c1 = "HOW"; // LINE1 > c1 doesn...

Pointers to pointers #3
What do pointers to pointers accomplish? How does having a pointer to a pointer help a design or aglorithm? Where are they normally used? cman In article <1171819744.426037.176610@s48g2000cws.googlegroups.com>, cman <tilakb@gmail.com> wrote: >What do pointers to pointers accomplish? How does having a pointer to >a pointer help a design or aglorithm? Where are they normally used? > >cman > If you want to modify an object passed in the argument list of a function from within the function, you must pass a pointer to the object as the argument. So, if you want to mo...

pointer to a pointer problems
An open book test I am taking, tasked me to write a function with the following prototype. Yea, its a test, so don't give me the implementation. But it is open book, so maybe I can at least get a few questions answered. // returns the next token in a string found seperated by the delimeter. upon return updates str to point to the next token in // the string after the one returned const char * nextToken(const char ** str, const char delimeter); When I look at this, it looks very c-style to me. I think it is unsafe. 1) I don't see a way to return a different string without allocating ...

Pointer to Pointer to character
a simple program... #include <iostream> using namespace std; int main(int argc, char** argv) { cout<<argv<<endl; cout<<**argv<<endl; cout<<argv[0][0]<<endl; cout<<&argv[0]<<endl; cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]? } why &argv[0] & &(argv[0][0]) difference? Howachen posted: > #include <iostream> You need to include "ostream" if you want to use "endl". > using namespace std; > > int main(int argc, char** argv) { > > ...

pointer to pointer question
struct pair_node { int i; double d; }; struct pair_node **x; The following is what I want to create. x -> [ ] -> (2,0.1) (3,0.2) (-1,?) [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?) [ ] -> (1,0.4) (-1,?) [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?) [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?) // Do I need an allocation before this loop for x? for(i = 0; i < N; ++i){ //dumps core on the following line, anyone can help me why? // am i missing something? nprob.x[i] = (struct pair_node *)malloc(sizeof(struct pair_node)*D); for(j = 0; j ...

Web resources about - pointer vs pointer to pointer - comp.lang.c

Plaxton Pointer - Wikipedia, the free encyclopedia
The Plaxton Pointer (originally known as the Reeve Burgess Pointer , and later as the TransBus Pointer and Alexander Dennis Pointer ) is a successful ...

Michael Pointer (@michaelpointer) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Thread: I'm looking for pointers to a Google Maps API overview.
... outline for a map, making it super-easy to edit, and also to have it in the most convenient form for someone browsing over the data. Any pointers ...

Pointer for iPhone, iPad, and iPod touch on the iTunes App Store
Get Pointer on the App Store. See screenshots and ratings, and read customer reviews.

Kobe Bryant 45 Points (NBA Record 12 Three-Pointers) vs Seattle Super Sonics - Highlights 07/01/2003 ...
Los Angeles Lakers vs Seattle Super Sonics 07/01/2003 Ignore Tags : mix derrick rose mix miami heat new york knicks mix thunder lebron james ...

Shark warning as white pointers cruise off Perth beaches
Large schools of salmon are bringing danger to Perth's beaches and waters as Great White Sharks follow the fish along the coast.

Footy kicks logic for a nine-pointer
Apart from hilarious change strips, footy’s number one growth industry appears to be inexplicable phenomena

Inside Mail: All the big bets, market movers, work whispers, trial pointers and quaddies for Canterbury ...
ALL the big bets, market movers, work whispers, trial pointers and quaddie tips for Canterbury and analysis of the Group 1s at Flemington on ...

Perth fisherman captures chilling footage of white pointer shark
... joked “the fish became the fisherman” after a close encounter with a shark. Mark Peacock captured chilling footage of the white pointer ramming ...

Canterbury Bulldogs coach Des Hasler calls victory over Melbourne Storm a four-pointer
Bulldogs coach Des Hasler described the victory over the Storm in Melbourne as a "four-point" win.

Resources last updated: 3/24/2016 4:02:00 PM