LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-29-2005, 09:07 AM   #1
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
Angry returning data to main() via a pointer to a pointer.


Hello

When my function storefiles() returns to main ,the names of the files that it should have stored in my arguement vector are not there,or at most only one is there.

As you can see in storefile() there is a for loop that steps along the arguement vector to make sure all the files that should be there are there.
This works fine.

But if i do the same thing in main() once storefiles has returned, none;or maybe one or two of the filenames are there ??.

I was thinking about making "store" external but I thought pointers negated the need for this?

I added retrun store line ,equating it to the store variable in main() thinking that realloc was moving the storage to a different place,but this didn't help

thanks in advance.

Code:
char **storefiles(struct dirent *dS,char **store,int count,int len)
{

	int c;
	c=count+2;
	if((store=(char**)realloc(store,sizeof(char *) * c))==NULL)
	return -1;
	if((*(store+count)=(char *)malloc(sizeof(char *) * len))==NULL)
	return -1;
	*(store+count)=strcpy(*(store+count),dS->d_name);
	*(store+count+1)=NULL;
	for(c=0;*(store+c) !=0;c++)
	printf("%s found.\n",*(store+c));
	return store;
	

	
}
 
Old 05-29-2005, 08:17 PM   #2
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
I think you've confused your use of store. I'd have declared it as char *store[] and used it as *store[n] to load the array (think of it as argv). It's late and my brain's not fully functional, but hopefully this'll give you a clue to get you moving again.

Steve
 
Old 05-30-2005, 04:40 AM   #3
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
ello..

I thought the syntax **buffer and *buffer[] were the same...!???

oh well..

I will give it a bash.

ta.
 
Old 05-30-2005, 02:20 PM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
I split up the re-sizing of the buffer **store from the copying of the file name from dS->d_name to *store.

I think its cleaner.

I think the problem was because I was'nt explicitly equating the memory blocks in main() to the reallocated memmory blocks in my function??!?!!

I have done this with an explicit return and cast.

It has definitely given me food for thought anyway...
I also used the syntax suggested by eddiebaby1023 in the calling function in main() which does clean things up I think.
Although I didn't change the declaration. ;0).

Anyway it works now.

cheers

Robert




Code:
main()
{

int n;
char **filelist;
....

filelist=newbuFFER(filelist,n)

......

filelist[n]=save(filelist[n],dStruct->d_name)   ....

.....
}




.........
	

char **newbuFFER(char **store,int count)
{
	int n;
	n=count+2;
	
	if ((store=(char **)realloc(store,sizeof(char **) * n))==NULL)
	return NULL;
	
	return (char **)store; 
}




char *save(char *store,char *source)
{

	int len;
	len=strlen(source)+1;
	if((store=(char *)malloc(sizeof(char *) * len))==NULL)
	return NULL;
	store=strcpy(store,source);
	return (char *)store;
	
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is pointer a data-type or what aditya Programming 39 02-09-2014 01:27 AM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 02:36 PM
disconnect USB mouse data from the mouse pointer Yeronimo Programming 0 04-07-2005 04:26 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 07:40 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 11:26 AM


All times are GMT -5. The time now is 12:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration