Thread: Pointers

  1. #1
    j_spinto
    Guest

    Pointers

    I dont know this well.

    when we create an array, and we pretend to get its conteud we must do for example printf("%c", *array) or printf("%c", array) ?
    its the same thing doing char array[] and char array?
    i never understood this.
    thanks

  2. #2
    j_spinto
    Guest
    #include <stdio.h>

    int my_array[] = {1,23,17,4,-5,100};
    int *ptr;
    Code:
    int main(void)
    {
        ptr = &my_array[0];     /* its the same thing writing &my_array[0] or my_array isnt it? so ptr=my_array */
        for (int i = 0; i < 6; i++)
        {
          printf("my_array[%d] = %d   ",i,my_array[i]);
        }
        return 0;
    }
    if we want to print the whole array we should do
    Code:
          printf("my_array = %d", *my_array);
    ????

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    This is an excerpt from my explnation of multidimensional arrays (http://cboard.cprogramming.com/showthread.php?t=64947)

    I thought maybe it would help

    let's look at at one dimensional array. (let's pretend that an int is one byte, which it isn't)

    int a[10];

    this reserves 10 bytes of memory. "a" is really a pointer (again, some may argue, but for the sake of simplicity, let's not).
    it points to that first byte in memory that is reserved for the array.
    all the bytes are guarenteed to be in sequential order when the
    array is declared as such. when you ask for a[5], the compiler
    finds the address that "a" points to. then, it will move "up" 5
    bytes in memory and return the value stored there. a[5] is the
    same as *a+5. "a" is referred to as a segment. the five is referred to
    as the offset. in short, a[5] and *a+5 says give me the value that is stored in
    five bytes past "a".
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    j_spinto
    Guest
    so:
    a[5]=*a+5
    its the same thing to do char *a than do char a[] isnt it?
    * says a[0]
    ** says a[0][0]
    *** says a[0][0][0]
    ***a+1 says a[0][0][1]
    this helps much
    thank you

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by j_spinto
    a[5]=*a+5
    its the same thing to do char *a than do char a[] isnt it?
    char *a != char a[n]
    char *a only allocates memory to store a memory address
    char a[n] allocates memory to store characters in

    in most cases, though, subscripting (using []) and pointer notation are interchangeable, but not necessarily the same.

    i forgot to copy the disclaimer when i copied the excerpt...it basically said i was going for logic, not techinicality in my explanation. before imprinting anything into your mind, i suggest reading preludes discussion on pointers vs arrays found here http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Last edited by misplaced; 05-01-2005 at 04:30 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    j_spinto
    Guest
    yeah i know, i read the entire post and i know that ints never reserve 1 byte memory only

    But look, if we do like this:
    Code:
     char b[]="this is a string";
     char *a="this is a string";
    a=b isnt it?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >a=b isnt it?
    No, not on any level except for string comparison. a is an array sized and initialized to "this is a string". b is a pointer to the string literal "this is a string". Neither the objects nor the contained strings refer to the same memory locations.
    My best code is written with the delete key.

  8. #8
    j_spinto
    Guest
    i wasnt talking about having the same memory address but the same conteud

  9. #9
    j_spinto
    Guest
    int *b = malloc(2 * sizeof (*b)) is the same as int b[2] this one is correct isnt it? :X

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
     char b[]="this is a string"; // Can be manipulated using string functions in a normal way
     char *a="this is a string";  // Is stored in read only memory and can not be changed

  11. #11
    j_spinto
    Guest
    Thank you all. I now understand quite or less its up to me to know the rest
    great forum this
    Hugs

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by j_spinto
    int *b = malloc(2 * sizeof (*b)) is the same as int b[2] this one is correct isnt it? :X
    No. One is a dynamicly allocated block of memory, which can be used after the function ends, assuming you return it, and the other will be gone (unless declared static) when the function ends. (Assuming the second is in fact declared in a function. The first has to be declared in a function, at least the way you have it.) Also the first one must be freed after you're done with it. The latter will automaticly be destroyed when it goes out of scope. Also, while array names act like pointers in most cases, they aren't.

    However, if you're wondering if they're both effectively two integers, yes.

    You should read THE FAQ for arrays and pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    j_spinto
    Guest
    I saw it in the faq lol :P

  14. #14
    j_spinto
    Guest
    i am using bloodshed
    Code:
    int main(void) {
        char *array;
        array="this is a string";
        printf("%s\n", array);
        system("pause");
        return 0;
    }
    using the * instead of [] allows me not to define the reserved space for the string
    if i use it do it like this, with the [] instead of *:

    Code:
    int main(void) {
        char *array;
        array="this is a string";
        printf("%s\n", array);
        system("pause");
        return 0;
    }
    it gives me an error

    i have been trying many small apps such as this and i concluded:
    - when we are printing a simple array we can never use the * in the printf;
    - we cant do *array="string"; but just array="string";
    this may help for noobs like me

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Um... both of those sample pieces of code are the exact same thing. I'm sure you meant them to be different some how, but they're not.

    Furthremore, you seem to be having problems with pointers and arrays. As stated, I suggest again that you go and read the Array / Pointer FAQ that I linked to. You may have "saw" the FAQ. But you most definately haven't read the FAQ. Or, if you have, you definately do not understand the FAQ, because your statements make that clear.

    1) You can never assign strings to arrays with the assignment operator. You must copy them an element at a time, or use a function which does the exact same thing (strcpy for example).

    2) This is not an array:
    Code:
    char *array;
    It is a pointer to a character, which in this example, points some place randomly. What you're doing here:
    Code:
    array = "this is a string literal, not an array";
    Is assigning the address of a string literal to the pointer. If you change the pointer in any way, this literal is then lost, unless you're keeping track of it with another pointer. You cannot modify string literals in any way. You do not free string literals like you would with dynamic memory allocation + copying something to it.

    Again, this is not an array in any way shape or form.

    3) This of course is wrong:
    Code:
    *array = "this is a string literal";
    Because the * denotes dereferncing of the pointer, which is in this case entirely wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

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