C language > Beginner questions

Pointer Problem?

<< < (2/2)

Max_Power_Up:
Well, thank you very much for tht insight :)

The reason Im doing it like this is because I was using a structure pointer and it
still didn't work, thats why I tried doing it this way, would I have to define
a RGBT ** , in order to point to a structure array , or can I just use the first
member and hope the sequential access will work?
Hey - I followed your advice closely - the problem is now fixed :) ,
I am discovering other bugs though lol! So the PellesC OpenPlayer has
to take a back seat till I can track down those bugs and
squash them - but the advice on passing a struct pointer was perfect
thank you :)

This is the application Im working on :

frankie:
An array is always referred as the address of the first element, also for multidimensional arrays. So passing a pointer to an object, int or char or a structure, acts like a pointer to a single element if you use it directly, or as an array of elements if you access them with array notation:

--- Code: ---int *Int;
struct foo{
  char a;
  char b:
  char c;
};
struct foo *MyFoo;

void test(void)
{
  int i;

  MyFoo = malloc(sizeof(struct foo) * 100);  //Allocate dynamically an array of 100 structs
  Int      = malloc(sizeof(int) * 100);           //Allocate dynamically an array of 100 int's

  for (i=0; i<100; i++)
  {
     Int[i] = i;
     MyFoo[i].a = Int[i] / 2;
     MyFoo[i].b = Int[i] / 4
     MyFoo[i].c = Int[i] / 8;
  }
}

--- End code ---
You cannot pass a poiter to an array statically declared:

--- Code: ---int i[100];
  .....
  test(&i);     //ERROR!!!
  test(i);       //OK! The name of a static array is a pointer to the first element
  test(&i[0])  //OK! We are passing the address of first element

--- End code ---
A definition like:

--- Code: ---  char **p;
--- End code ---
defines a pointer of pointers meaning that an object is a pointer to another pointer that in turn points to the object, or could mean that you have an array of pointers that points to an array of elements (the address of element 0 of each array):

--- Code: ---  char **p;
  char message1[16] = "hello"
  char message2[16] = "C language"
  char message3[16] = "world!"

void foo(void)
{
   p = malloc( 3 * sizeof(char *));  //Allocate the array of pointers (3 elements)
   p[0] = message1;
   p[1] = message2;
   p[2] = message3;
}

--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version