Closed Thread
Results 1 to 8 of 8

Thread: Getting one array index to point to another array index

  1. #1
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Getting one array index to point to another array index

    If I have this code:

    Code:
    #include <iostream>
    using namespace std;
    
    int
    main(int argc, char *argv[])
    {
    	char a[1];
    	char b[4];
    	a[0] = 3;
    	b[0] = 1;
    	b[1] = 2;
    	*b[2] = a[0];
    	b[3] = 4;
    	a[0] = 5;
    	cout << b[2] << endl;
    	return (0);
    }
    How do I make b[2] point to a[0], so that if I change the value of a[0], the value of b[2] gets changed accordingly.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Getting one array index to point to another array index

    You'd need a pointer to be able to point.

  4. #3
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: Getting one array index to point to another array index

    the b[2] is always evaluated to be the value contained inside the b index of 2 not as a pointer and since b is a character array it cannot be used as a pointer.

    the b is considered to be a special type of pointers but it always points to the beginning of the array and cannot point to next element.
    so i dont think that's possible to do what you want.
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  5. #4
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Getting one array index to point to another array index

    Alright, so instead I need to make an array of 4 pointers. And I want to be able to set the value of the memory group for each pointer. So I do this:

    Code:
    #include <iostream>
    using namespace std;
    
    int
    main(int argc, char *argv[])
    {
    	char a[1];
    	char *b[4];
    	a[0] = 3;
    	*b[0] = 1;
    	*b[1] = 2;
    	*b[2] = &a[0];
    	*b[3] = 4;
    	a[0] = 5;
    	cout << a[0] << endl;
    	cout << b[0] << endl;
    	cout << b[1] << endl;
    	cout << b[2] << endl;
    	cout << b[3] << endl;
    	return (0);
    }
    I know that *b[0] works as I want it to, by assigning the allocated memory pointed to by *b[0] the value 1. And *b[2] = &a[0] should assign the address of a[0] to the pointer *b[2]. But I get the error "cannot convert from 'char *' to 'char'". So what do I need to change here?

  6. #5
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: Getting one array index to point to another array index

    try it without the *
    so it would be b[2]=&a[0]

    as the *with a pointers is used to access the value that the pointer points at, but without * let you reassign an address to the pointer variable.
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  7. #6
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Getting one array index to point to another array index

    Right, that compiles, but I get a warning for *b[0] = 1; saying "uninitialized local variable 'b' used". So how do I initialize it?

  8. #7
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Getting one array index to point to another array index

    You kinda need to decide what you want b to be. Is it going to be an array of pointers, or an array of chars? It can't be both simultaneously.

  9. #8
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Getting one array index to point to another array index

    Well, obviously I want it to be an array of pointers that point to variables of type char like with *b[0] = 1;

    Edit:
    Nevermind, I neglected the fact that *b[0] = 1 means that at the address that b[0] points to, the value is set to 1. So it makes sense now that b[0] has to be initialized to point to an address first, and that's why I'm getting the error.

    Thank you for all the help, I think I've got it now.
    Last edited by ThemePark; 01-25-2010 at 12:51 AM.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. using a variable as array index
    By jackson6612 in forum C and C++
    Replies: 4
    Last Post: 10-09-2011, 01:18 PM
  2. an array with char index
    By chipbu in forum C# Programming
    Replies: 2
    Last Post: 04-12-2010, 03:46 PM
  3. Array Index Out Of Bound
    By lovemarshall in forum Java Help
    Replies: 3
    Last Post: 03-08-2010, 12:15 PM
  4. removing index of array of no values
    By mhadidi2002 in forum Perl
    Replies: 3
    Last Post: 06-17-2009, 02:45 PM
  5. Using array index
    By gaylo565 in forum C# Programming
    Replies: 3
    Last Post: 05-16-2008, 09:19 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts