Like Tree3Likes
  • 1 Post By seemaxie
  • 2 Post By std10093

multiple pointers

This is a discussion on multiple pointers within the C Programming forums, part of the General Programming Boards category; hello everyone I have a stupid question.When I have allocated a Code: char* a; to point to an array of ...

  1. #1
    quo
    quo is offline
    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 ;
    quo likes this.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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.
    seemaxie and quo like this.

  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,675
    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, 04: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, 01:57 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21