How can we demonstrate interchangeability of Arrays and Pointers in a structure?

This is a discussion on How can we demonstrate interchangeability of Arrays and Pointers in a structure? within the C Programming forums, part of the General Programming Boards category; We know that arrays and pointers in C are interchangeably. Namely; Code: //Pseudocode for giving instance. int* bPtr; int b; ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48

    How can we demonstrate interchangeability of Arrays and Pointers in a structure?

    We know that arrays and pointers in C are interchangeably. Namely;

    Code:
    //Pseudocode for giving instance.
    int* bPtr;
    int b;
    
    bPtr=&b;
    
    *(bPtr+i)  | bPtr[i]
    
    *(b+i)      | b[i]
    So I have tried to perform this knowledge to the structures. But I've failed. How can I overcome this problem? Or can't we do this in the structures? Or is there a special case in the structures?


    Here the programme which runs:
    Name:  1.png
Views: 169
Size:  7.1 KB
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct card{
        char *face;
        char *suit;
    };
    
    
    int main()
    {
        struct card aCard;
        struct card *cardPtr;
    
    
        aCard.face="Ace";
        aCard.suit="Spade";
    
    
        cardPtr = &aCard;
    
    
        printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
                                          cardPtr->face," of ",cardPtr->suit,
                                          (*cardPtr).face," of ",(*cardPtr).suit);
    
    
        return 0;
    }
    Here the programme which doesn't run.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct card{
        char face[50];
        char suit[50];
    };
    
    
    int main()
    {
        struct card aCard;
        struct card *cardPtr;
    
    
        aCard.(face[])="Ace";
        aCard.(suit[])="Spade";
    
    
        cardPtr = &aCard;
    
    
        printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
                                          cardPtr->face," of ",cardPtr->suit,
                                          (*cardPtr).(face[])," of ",(*cardPtr).(suit[]));
                                        //(*cardPtr).face[]," of ",(*cardPtr).suit[]);
        return 0;                       //(*cardPtr).face," of ",(*cardPtr).suit);
    }
    Last edited by hefese; 07-28-2012 at 05:50 AM. Reason: Adding a picture about output.

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    This should solve your problem Name:  verde.gif
Views: 174
Size:  349 Bytes:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
      
      
    struct card{
        char face[50];
        char suit[50];
    };
      
      
    int main()
    {
        struct card aCard;
        struct card *cardPtr;
     
     
        strcpy(aCard.face, "Ace");
        strcpy(aCard.suit, "Spade");
     
      
        cardPtr = &aCard;
      
      
        printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
                                          cardPtr->face," of ",cardPtr->suit,
                                          (*cardPtr).face," of ",(*cardPtr).suit);
     
     
        return 0;
    }


    I hope that I have helped Name:  smiled.gif
Views: 165
Size:  176 Bytes.
    Last edited by rjjj; 07-28-2012 at 07:43 AM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hefese View Post
    We know that arrays and pointers in C are interchangeably.
    No they are not: C FAQ/6.2

    Code:
    //Pseudocode for giving instance.
    int* bPtr;
    int b;
    
    bPtr=&b;
    
    *(bPtr+i)  | bPtr[i]
    *(b+i)      | b[i]
    b is an int and has no index. *(b + i) and b[i] will get you errors.
    *(bPtr + i) and bPtr[i] will return garbage because there is no array at the address of b.

    Code:
    aCard.(face[])="Ace";
    aCard.(suit[])="Spade";
    Assigning a string to a char-array only works in initialization:
    Code:
    char string[] = "This is a string";
    In your case you have to use strcpy or even better strncpy:
    Code:
    strncpy(aCard.face, "Ace", 50);
    Code:
    (*cardPtr).(face[])," of ",(*cardPtr).(suit[])
    From where have you got this weird syntax?

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by rjjj View Post
    This should solve your problem Name:  verde.gif
Views: 174
Size:  349 Bytes:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
      
      
    struct card{
        char face[50];
        char suit[50];
    };
      
      
    int main()
    {
        struct card aCard;
        struct card *cardPtr;
     
     
        strcpy(aCard.face, "Ace");
        strcpy(aCard.suit, "Spade");
     
      
        cardPtr = &aCard;
      
      
        printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
                                          cardPtr->face," of ",cardPtr->suit,
                                          (*cardPtr).face," of ",(*cardPtr).suit);
     
     
        return 0;
    }


    I hope that I have helped Name:  smiled.gif
Views: 165
Size:  176 Bytes.
    Thank you rjjj for the reply.

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


    Quote Originally Posted by AndiPersti View Post
    No they are not: C FAQ/6.2

    Code:
    //Pseudocode for giving instance.
    int* bPtr;
    int b;
    
    bPtr=&b;
    
    *(bPtr+i)  | bPtr[i]
    *(b+i)      | b[i]
    b is an int and has no index. *(b + i) and b[i] will get you errors.
    *(bPtr + i) and bPtr[i] will return garbage because there is no array at the address of b.

    Code:
    aCard.(face[])="Ace";
    aCard.(suit[])="Spade";
    Assigning a string to a char-array only works in initialization:
    Code:
    char string[] = "This is a string";
    In your case you have to use strcpy or even better strncpy:
    Code:
    strncpy(aCard.face, "Ace", 50);
    Code:
    (*cardPtr).(face[])," of ",(*cardPtr).(suit[])
    From where have you got this weird syntax?

    Bye, Andreas
    *It's so interesting. That is to say I have known wrong. Thank you so much for the link.
    *I forgot to write brackets for int b. (int b[]; )
    *I didn't know that "assigning a string to a char-array only works in initialization". Thank you for this knowledge.
    *And finally it's time to answer for your question: There is no place. I made it up.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    For these declarations:
    Code:
    int array[10];
    int index = 5;
    array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by iMalc View Post
    For these declarations:
    Code:
    int array[10];
    int index = 5;
    array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .
    *I thought that it was a joke. But then, I just stared with mouth open at the window output like .
    *Alright. I understand that I didn't know wrong this subject. I just knew nothing.
    *Thank you for sharing that something confusing. It's opened a new door for me about my study.
    Last edited by hefese; 07-29-2012 at 04:35 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by iMalc View Post
    array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .
    Just to be pedantic, array[index] and *(array + index) are not necessarily the same. They do give equivalent results, but the way they do that (say at the level of machine instructions) is often different. And that difference can be detected in various ways (performance measures, number of instructions to achieve an effect, etc).

    index[array] and array[index] are equivalent because addition is commutative for pointers and integral values (i.e. x + y and y + x are equivalent)
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to demonstrate strchr function
    By november1992 in forum C Programming
    Replies: 11
    Last Post: 05-04-2012, 01:52 PM
  2. How to structure my arrays?
    By worldder in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2010, 10:43 AM
  3. can some please demonstrate fgets ?
    By broli86 in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 02:41 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  5. structure pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 11:04 AM

Tags for this Thread


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