Thread: pointer to array

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Unhappy pointer to array

    Dear Sir,
    I have a probleme in a program in which I cant send the address of a pointer to an array ,like this,

    char (*a)[10][10];
    char *temp;
    -----
    a[0]=hh ------
    a[1]=kk --------
    temp=a[0];
    a[0]=a[1];
    a[1]=temp;

    -------------
    -------------

    The output must be a[0]=kk
    a[1]=hh

    But there is an error in the last three steps,temp is not reciving the address of a[0],a[0] not reciving address of a[1].

    I dont know how to locate the error,what is the problem with the pointer temp.
    Plz remove my confiusion,
    Thank you.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What are hh and kk? Are those strings? Could you post the entire code segment?

    Meanwhile, you can assign the address of a pointer to another pointer (a pointer to a pointer) and use it like so:
    Code:
    {
      char *ptr = "this is a string";
      char **pptr = &ptr;
    
      printf("The string is: %s\n", *pptr);
      printf("The first character of the string is: %c\n", (*pptr)[0]);
      printf("pptr: %p, *pptr: %p, &ptr: %p, ptr: %p\n",
        pptr, *pptr, &ptr, ptr);
    }
    Which should output:
    The string is: this is a string
    The first character of the string is: t
    pptr: 0xbffff784, *pptr: 0x80484ac, &ptr: 0xbffff784, ptr: 0x80484ac
    You can see that the address pptr points to is the address of the ptr pointer.
    Last edited by itsme86; 07-26-2005 at 12:58 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Unhappy plz under

    To
    itsme86,
    cprogramming.
    Sir,
    First I will thank for my question.Plz try to understand me , i want a program which will swap the strings .

    For this reason we need pointers & i take here a pointer to array.

    here is the full code;
    Code:
    void main()
    {
    
    char (*a)[10][10];
    char *temp;
    int i;
    
        printf("enter the names");
    for(i=0;i<=1;i++)
        scnaf("%s",a[i]);
    
    temp=a[0];  /*assignig the add. of a[0] to temp*/
    a[0]=a[1];   /*assiging the add. of a[1] to a[0]*/
    a[1]=temp;   
    
    (HERE is an error in above swapping )
    
    
    pritnf("%s %s",a[0],a[1]);
    }
    _____________
    out put must be,

    enters the names nitin
    sharma

    sharma nitin
    ____________


    This is my probleme why I cant send the address of a pointer to array((*a)[10][10]) to pointer of type char(*temp).


    Thank you,
    nitin.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Paste code you compiled, not what you 'remembered'
    > scnaf("%s",a[i]);
    > pritnf("%s %s",a[0],a[1]);
    This is just a typo-fest

    > char (*a)[10][10];
    Start with char a[10][10];
    And char temp[10];

    Then the swapping would be
    strcpy( temp, a[0] );
    strcpy( a[0], a[1] );
    strcpy( a[1], temp );

    Oh, and main returns int, not void
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    i want a program which will swap the strings
    Code:
    int main(void) {
        char *s1, *s2, *p;
    
        /* initialize s1 and s2 */
    
        printf("\n%s\n%s\n", s1, s2);
        p = s1, s1 = s2, s2 = p;
        printf("\n%s\n%s\n", s1, s2);
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    void main() wats the big deal in this?.it doest matter wat main returns.i know it returns 0 if execution was successful and else otherwise.

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Exactly. It returns EXIT_SUCCESS if successful, otherwise it returns EXIT_FAILURE. It matters to people like me, who like to type commands like
    ./configure && make && sudo make install
    on the Linux prompt. You'll have difficulty returning anything from a void main().
    Away.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cbastard
    void main() wats the big deal in this?.it doest matter wat main returns.i know it returns 0 if execution was successful and else otherwise.
    Ignore this poor attempt at trolling. If you're really that ignorant, just read the FAQ. Nothing to see here, move along.


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

  9. #9
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by nitin1
    To
    itsme86,
    cprogramming.
    Sir,
    First I will thank for my question.Plz try to understand me , i want a program which will swap the strings .

    For this reason we need pointers & i take here a pointer to array.

    here is the full code;
    Code:
    void main()
    {
    
    char (*a)[10][10];
    char *temp;
    int i;
    
        printf("enter the names");
    for(i=0;i<=1;i++)
        scnaf("%s",a[i]);
    
    temp=a[0];  /*assignig the add. of a[0] to temp*/
    a[0]=a[1];   /*assiging the add. of a[1] to a[0]*/
    a[1]=temp;   
    
    (HERE is an error in above swapping )
    
    
    pritnf("%s %s",a[0],a[1]);
    }
    _____________
    out put must be,

    enters the names nitin
    sharma

    sharma nitin
    ____________


    This is my probleme why I cant send the address of a pointer to array((*a)[10][10]) to pointer of type char(*temp).


    Thank you,
    nitin.
    thats one of the possible answer.ur statements a[0]=a[1] is wrong i think.thats the difference between pointer and array.u can not use array pointer to left side of assignment operator.
    int main()
    {

    char a[10][10];
    char temp[10];
    int i;

    printf("enter the names");
    for(i=0;i<=1;i++)
    scanf("%s",a[i]);

    strcpy(temp,a[0]); /*assignig the add. of a[0] to temp*/
    strcpy(a[0],a[1]); /*assiging the add. of a[1] to a[0]*/
    strcpy(a[1],temp);


    printf("%s %s",a[0],a[1]);

    return 0;
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use code tags.

    What's this for?
    Code:
    for(i=0;i<=1;i++)
    [edit]
    Never mind, I see it's supposed to be i<=10.
    [/edit]
    Last edited by dwks; 07-27-2005 at 05:09 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    u can not use array pointer to left side of assignment operator
    Yeah, you should use a for loop or memset or strcpy or something else.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, I don't think you're supposed to change where an array points to. That is,
    Code:
    char *p, *q;
    /* ... */
    p = q;
    is valid, but
    Code:
    char p[100], *q;
    /* ... */
    p = q;
    is not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM