Thread: a small help here

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    184

    a small help here

    hello guys,
    i had a little problem in understand the pointer to pointer reference concept (i.e **p) how it is used. and what is the effect of uisng that.

    any help will be appreciated

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Try reading through this post. It was discussed decently there. Let me know if you have any questions.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Think of **p as p[][]. A multidimensional array (even though technically it's not).

    Code:
    const char **p = {
        "Apples",
        "Cornbread"
    };
    
    // p[] would carry the strings
    // p[][] would carry the characters of the strings
    
    // *p would carry the strings
    // **p would carry the characters of the strings
    
    p[0][0] = 'B' // Change Apples to Bpples 
    **p = 'C'      // Change Bpples to Cpples

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Does any of that work?
    I very much doubt it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean because of const, or because they're string literals. Or both.

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

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This is crazy horse !

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void )
    {
       // Create a string array
       
       char p[2][100];
    
       // Copy the strings into the string array
    
       strncpy( p[0], "Apples", sizeof( *p ) );
       strncpy( p[1], "Corn Bread", sizeof( p[0] ) );
    
       // Next is the same thing as:
       // **p = 'B'
       
       p[0][0] = 'B'; // Change Apples to Bpples
       puts( p[0] );
    
       // Next is the same thing as:
       // p[1][5] = 'X'
       
       *( ( *( p+1 ) )+5 ) = 'X';      // Change Corn Bread to Corn Xread
       puts( p[1] );
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 03:09 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM