Thread: multiple pointers

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    multiple pointers

    hello everyone

    I have a stupid question.When I have allocated a

    Code:
    char* a;
    to point to an array of chars.And then I want a

    Code:
    char* b;
    that I want it to point where the pointer a does.How do I do that?
    Like this:

    Code:
    a=b;
    and with this way pointer b doesn't need malloc right?

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    b = a ;

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think this very simple example will explain everything
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char* a;
        char* b;
        char array[6];
        array[0]='s';
        array[1]='a';
        array[2]='m';
        array[3]='a';
        array[4]='r';
        array[5]='\0';
    
        a = array; /*set pointer a to array*/
        /* the above is equivalent to to setting pointer a to the address of the first element of the array*/
        for( i = 0 ; i < 6 ; i++)
           printf("%c\n",a[i]);
        b = a; /*set pointer b to pointer a*/
        printf("Now b\n");
        for( i = 0 ; i < 6 ; i++) /* guess what this will print/*
           printf("%c\n",b[i]);
        return 0;
    }
    for better understanding of pointer just draw the memory in a paper
    Last edited by std10093; 10-25-2012 at 02:46 AM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    @std i think array should be size 6 . No place for '\0'

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by seemaxie View Post
    @std i think array should be size 6 . No place for '\0'
    Excellent input! I will now edit my code for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-02-2011, 07:27 PM
  2. Replies: 4
    Last Post: 03-11-2005, 05:45 PM
  3. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  4. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM