Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create an array of pointers

Status
Not open for further replies.

Maulkin

Programmer
Apr 8, 2002
19
0
0
AU
How would I create an array of pointers, where each pointer in the array points to a character string in another array?


any help would be vastly appreciated
 
#include <stdio.h>

#define ELEMS 5

int main( void )
{
int i;
char* aOfs[ELEMS]={ &quot;array&quot;,
&quot;of&quot;,
&quot;pointers&quot;,
&quot;tek&quot;,
&quot;tips&quot;
};
char* aOfp[ELEMS];

for( i=0;i<ELEMS;i++ )
aOfp=aOfs;

for( i=0;i<ELEMS;i++ )
printf( &quot;%s\n&quot;,aOfp );

return 0;
} Mike L.G.
mlg400@linuxmail.org
 
thanks for the help

this will not work in my problem my string array ispopulated by a loop and looks like this if input strings are &quot;one&quot; &quot;two&quot; &quot;three&quot;
on the first pass the array will contain
oneNULL
on the second
oneNULLtwoNULL
on the third
oneNULLtwoNULLthreeNULL I need the pointers to point to the

O in One the T in Two and the T in Three
is this possible.
 
Not too sure what you are getting at, but I'll give it another go...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define ELEMS 5
#define ELEMS_X 20

int main( void )
{
int i;
char matrix[ELEMS][ELEMS_X];
char* aOfp[ELEMS];
char* aOfs[ELEMS]={ &quot;One&quot;,
&quot;Two&quot;,
&quot;Three&quot;,
&quot;Four&quot;,
&quot;Five&quot;
};


for( i=0;i<ELEMS;i++ )
strcpy( matrix,aOfs );

for( i=0;i<ELEMS;i++ )
aOfp=matrix;

for( i=0;i<ELEMS;i++ )
{
printf( &quot;%c - &quot;,aOfp[0] );
printf( &quot;%s\n&quot;,aOfp );
}

return 0;
}

1. The first for loop inserts the strings into a 2d array.

2.The second loop sets an array of char pointers to point to each string in matrix[][]

3.The last loop prints out the first char and the entire string via the array of pointers. Mike L.G.
mlg400@linuxmail.org
 
My problem is similar to this one.

I too need to create array of pointers.
char* aOfs[]={ &quot;One&quot;,
&quot;Two&quot;,
&quot;Three&quot;,
&quot;Four&quot;,
&quot;Five&quot;
};
In the above declaration, I don't have ELEMS as it was. So i don't know the total strings in the array. I need to point each one of those strings and display the two strings on the LCD two at the time.

thanks in advance
HKC
 
To be able to do what you want to do you will need an index for your array,you might use &quot;#define ELEMS 5&quot; like in the previous example or something like &quot;const int ELEMS = 5&quot;.But without an index you wont be able to achieve it.

let say that you have one,now to print the strings two at the time,you could use this:

int i = 0;
while( i < ELEMS )
{
if( i < ELEMS - 1 )
{
printf(&quot;%s&quot;,aOfs[i++]);
printf(&quot; %s\n&quot;,aOfs[i++]);
}

else
{
printf(&quot;%s&quot;,aOfs[i++]);
}
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top