pointer and 2d array

This is a discussion on pointer and 2d array within the C Programming forums, part of the General Programming Boards category; If I want to access an individual character in a pointer to an array (*argv[3]) can you access it by ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    4

    pointer and 2d array

    If I want to access an individual character in a pointer to an array (*argv[3]) can you access it by means of a 2d array. Say I wanted the 4th character of argv[3], is it as simple as argv[3][3] or do I need to move to contents of argv[3] to a single D array and access it that way? Thank you in advanced.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    That's actually an array of pointers, not a pointer to an array. And yeah you can just do argv[3][3];
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    It is as simple as argv[3][3].

    e;fb

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    4
    Thanks. Yea I remember the name for it just a couple minutes ago but a name's a name. :P

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    4
    One more thing. This is a small thing I used to test how each index of argv[] was terminated and I found something interesting that I didn't know.
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            int i;
            for ( i=0 ; i < 20; i++ )
                    printf("%c",argv[1][i]);
            printf("\n");
            return 0;
    }
    Just prints every character of the index of argv[i]. I tested it a couple times and here were my results.
    Code:
    ate@ncward:~/c$ gcc try.c -o try
    nate@ncward:~/c$ try cow
    cowCPLUS_INCLUDE_PA
    nate@ncward:~/c$ try cower
    cowerCPLUS_INCLUDE_
    nate@ncward:~/c$ try dddddddddddddddd
    ddddddddddddddddCPL
    nate@ncward:~/c$ try
    Segmentation fault
    nate@ncward:~/c$ try
    Segmentation fault
    nate@ncward:~/c$ try k
    kCPLUS_INCLUDE_PATH
    Im on Slack 10, 2.6.8 kernel and whatever gcc came with Slack 10. My question is simply that there is no way that this little function is portable ( maybe so I don't know ) but how are the indexs of argv[] terminated. I would just test until I hit the 'C' but I don't think this would be smart.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    They're terminated the same way every string is terminated in C, with a '\0'. You could rewrite your loop control like this:
    Code:
    for( i=0 ; argv[1][i] != '\0' ; i++ )
    Also, you might want to try adding something like this right before your loop:
    Code:
    if(argc != 2)
    {
      puts("Usage: try <word>");
      return 1;
    }
    This guarantees that the loop will only run if they pass a command line argument.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 09:15 PM
  3. Passing a pointer to a 2D array
    By OakRand in forum C Programming
    Replies: 8
    Last Post: 05-18-2006, 12:50 AM
  4. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM

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