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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple pointer dyn allocation

Status
Not open for further replies.

kellan4459

IS-IT--Management
Jul 3, 2003
84
US
I am trying to take in input no matter the number of spaces so that I get two numbers. If there are more than two numbers on the line I want to print error and exit program. The steps I was trying to take is to create a multidimensional pointer with pointers. However, I am not doing something right with the dynamic allocation
basically what I am looking to get is something that will take in each line
say
22 44
13 6
and put it into the pointers as the same input(similar to multidimensional array)
but if it is
22 44 6
print error and exit
I have the following code but getting several different errors
int **getin(void){
int scanfcheck, **holder, i=0;
char *buffer;
buffer = malloc(80);
holder=NULL;
if (buffer != NULL){
while(((scanfcheck=scanf("%s",buffer))!= EOF)){

int *temp=realloc(holder,(sizeof(holder)+sizeof(int)));
printf("holder size = %d\n",sizeof(holder));
printf("buffer = %s\n",buffer);
if(temp != NULL){
holder = temp;
holder=malloc(sizeof(buffer));
printf("sizeof holderI %d\n",sizeof(holder));
printf("inside if temp\n");


if (holder != NULL){
holder=(int*)buffer;
}
i++;
}
else
{
fprintf(stdout,"ERROR IN REALLOC");
break;
}

}}
else
{
fprintf(stdout,"ERROR IN MALLOC");
}
printf("i = %d\n",i);
while(i > 0){
printf("%s\n",(char *)holder);
i--;
}
return holder;
}
 
Phew. I looked at this somewhat, and find it very confusing. I find both the code and the statement of requirements confusing.

If you can be somewhat more specific, I will try to help.

As far as the requirements go, are you looking for an array of characters that contain text representations of the numbers, or are you looking for the binary (integer) values that correspond to these text representations? This is not clear from your comments or the code provided (I think that this is, however, perhaps one of the problems with this code). A binary (integer) representation of the value is different, and has to be converted from the text representation that you get from your input.

There's some seeming illogic in your code, for example:

Code:
 if(temp != NULL){
                holder = temp;
                holder=malloc(sizeof(buffer));

In this case the assignment
Code:
holder = temp
is pointless, since its effects are immediately undone in the next line.
 
I have tried several ways of doing this. I want to take in two numbers per line, no more no less. If more than two or less than two I want to quit because of bad input.
so input should be anything like
2 45
3 42
1 76
as long as two integers whitespace doesn't matter.
I want to store each set of numbers in a dynamic array because input could be forever so to speak
It would probably be easier to use a struct array to hold each value x and y but I'm not sure how to do this.
basically if the user inputs the following
1 34
2 45
5 75
my array would have
x y
1 34
2 45
5 75
but if they enter
1 4
2
it will error
or
1 4
1 3 6
it will error
Hope this makes more sense, I have since revamped the code to no avail

Thanks
 
Okay, I built some code that does what you say (however, I'm assuming that you want the input converted to binary
Code:
int
values, hence the
Code:
sscanf
conversion, cf. below).

Note, I would not do this this way, I would use C++ STL containers for this. I particularly don't like the container methodology I used, however I followed the hints in your code for this. Using C++ STL containers would be much less code, but much more explaining. Also this is a
Code:
C
forum :cool:.

Please note the
Code:
free
'ing of both the container contents of the container and the container itself.

Code:
#include <stdlib.h>
#include <stdio.h>

#define SUCCESS 0
#define FAILURE -1

struct Pair
{
	int first;
	int second;
};
struct Pair* create_integer_pair( int line_number, char* buffer )
{
	struct Pair* p = NULL;
	int count, a, b, c;
	/*	We attempt to convert three input variables (more than required),
		if we get anything other than two, this is an error: */
	if( ( count = sscanf( buffer, &quot;%d %d %d&quot;, &a, &b, &c ) ) != 2 )
		printf( &quot;line %d Incorrect number of values: %d \&quot;%s\&quot;\n&quot;, 
					line_number, count, buffer );
	else
	{
		/* Okay, so allocate memory: */
		p = (struct Pair*) malloc( sizeof( Pair ) );
		if( p == NULL )
			printf( &quot;Unable to allocate memory.\n&quot; );
		else
		{
			printf( &quot;line %d Using: \&quot;%s\&quot;\n&quot;, line_number, buffer );
			p->first = a;
			p->second = b;
		}
	}
	return p;
}
int process( void )
{

	/* Just some test data, some correct, some incorrect: */
	char *test_input_lines[] = {
									{&quot;10 11&quot;}, 
									{&quot;20 30 40&quot;}, 
									{&quot;31 41&quot;}, 
									{&quot;1&quot;},
									{&quot;32 42 52 62&quot;}, 
									{&quot;23 13&quot;}
								};
	int test_input_line_count = sizeof( test_input_lines ) 
								/ sizeof( *test_input_lines );

	struct Pair* pair;
	struct Pair** container = (struct Pair**) NULL;
	int container_count = 0, i;
	for( i = 0; i < test_input_line_count; ++i )
	{
		pair = create_integer_pair( i, test_input_lines[ i ] );
		if( pair != NULL )
		{
			/* Got valid input, reallocate the container to contain this: */
			container = ( struct Pair** ) 
							realloc( container, 
										( sizeof( struct Pair** ) * 
										( container_count + 1 ) ) 
									);
			if( container == NULL )
			{
				printf( &quot;Unable to allocate space for container.\n&quot; );
				return FAILURE;
			}
			else
			{
				container[ container_count ] = pair;
				container_count++;
			}
		}

	}
	/* Print out container variables: */
	for( i = 0; i < container_count; ++i )
		printf( &quot;Pair %d: [%d,%d]\n&quot;, i, container[ i ]->first, container[ i ]->second );

	/* free up the container's contents: */
	for( i = 0; i < container_count; ++i )
		free( container[ i ] );
	/* free up the container itself: */
	free( container );

	return SUCCESS;

}

int main( int argc, char* argv[] )
{
	return process();
}
 
A good solution for your problem is to use a linked list in which you store the pairs of numbers entered.
You add a node (allocate memory) in the list when you have two numbers entered.
Then you can pass this list to another process and free the memory allocated.
Use the scanf and sscanf to get the numbers:


int n1=0;
int n2=0;

char str1[80];
char str2[80];
scanf(&quot; %[^\n]&quot;,str1);
if ((sscanf( str1,&quot;%d %d&quot;,&n1, &n2)== 2)
// add (n1,n2) in the list
else
// error message


-obislavu
 
>obislavu
A good solution for your problem is to use a...

Yes I had originally said that I would not do this the way that I did it, except that this approach relied on the general approach of the questioner. (Context sensitive, or something).

I'm familiar with a variety of approaches here. You suggest a linked list, but neither of us can tell how this data is to be used. So, you say 'linked list', and I say 'map' -- just for example.

Let's ask the original questioner: &quot;what are you using this data for?&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top