GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2004, 08:55
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road

Help on passing in arrays in functions?


Hi,

I''m new to C and dunno why the full char array buffer doesn't get passed into method. Run the following program, type "prompt bash" as input. you'll see that prompt gets split up into "pro"...sth. This is because the size of buffer is only 4 instead of 256.

Thanks.

P.S : i hope the code works, coz i'm takin snippets from my file.

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void Prompt(int flag, char buffer[256]);  //function to print prompt.

int main(int argc, char *argv[]) {

  char buffer[256];
  Prompt(0,buffer);
  return 0;

}//main

void Prompt(int flag,char buffer[256]) {  
  
  char prompt[256];
  char *token;
  
  strcpy(prompt,"mysh");
        
  while (1) {
    printf("buffer size: %d\n", sizeof(buffer));   //why isn't it 256?    
    printf("%s>", prompt);
    if (flag == 0)
      fgets(buffer, sizeof(buffer), stdin);  

    token = strtok(buffer, seps);
    printf("%s\n", token);

    if (token == NULL)   //<newline> command
      continue;

    //prompt
    else if (strcasecmp(token,"prompt") == 0) {
      strcpy(prompt,changePrompt());
    }//else if
        
  }//while

}//Prompt

char *changePrompt () {
  char *prompt = strtok(NULL, seps);
  if (prompt == NULL)
    return "\0";
  else 
    return prompt;
}//changePrompt
  #2  
Old 01-Apr-2004, 09:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi nustu:

One major problem that I see is your use of fgets:

Try putting this check into your program somewhere:

CPP / C++ / C Code:
printf("Sizeof buffer: %d\n",sizeof(buffer));

I bet you didn't get the result you wanted. Sizeof is simply going to return the sizeof your variable in bytes, (probably 4).

So when you use:
CPP / C++ / C Code:
fgets(buffer, sizeof(buffer), stdin);

It will only read in the first 4 charecters of your input. You need to specify the real size. Alot of times, I will use a #define for this like
CPP / C++ / C Code:
#define MAXSIZE 256

Then in the fgets:
CPP / C++ / C Code:
fgets(buffer, MAXSIZE, stdin);

There are some other things that I am not 100% sure on either. Unfortunately, your code did not compile as is, mostly due to no definition of seps.

HTH,
d
  #3  
Old 01-Apr-2004, 09:22
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road
Thanks, dsmith. I got it. The reason why it outputs 4 is because 4 bytes is used for a pointer.
  #4  
Old 01-Apr-2004, 09:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by nusstu
Thanks, dsmith. I got it. The reason why it outputs 4 is because 4 bytes is used for a pointer.

Yeah, you were on the right track to figuring it out when I replied. I notice that you changed your original post a bit, before I could reply, so I think you would have figured this out pretty quick.

A quick note as well, strtok is not well thought of by some. Here is a quote from the man page of strtok
Quote:
SYNOPSIS
#include <string.h>

char *strtok(char *s, const char *delim);

char *strtok_r(char *s, const char *delim, char **ptrptr);

...

BUGS
Never use these functions. If you do, note that:

These functions modify their first argument.

These functions cannot be used on constant strings.

The identity of the delimiting character is lost.

The strtok() function uses a static buffer while parsing, so it's
not thread safe. Use strtok_r() if this matters to you.

This may not be a big deal for you, but you may want to keep this limitations in mind.

Cheers,
d
  #5  
Old 01-Apr-2004, 11:55
meet_raman meet_raman is offline
Junior Member
 
Join Date: Mar 2004
Posts: 34
meet_raman is on a distinguished road

how to pass this


how do we pass two-dimentional integer array into functions and also how to return a 2-d integer array??

i pass a 1-d array by reference like this:
CPP / C++ / C Code:
main()
{
   <SOME CODE>
   
   int a[10];
   some_function(&a)

   <SOME CODE>

}

some_function(int *a)
{
    <PERFORM CALCULATION>

}
//no need to return obviously
how do we pass an array by value, the whole array at once??

plz guide...

(am i a novice?? OH HELL YEA..!!)
  #6  
Old 01-Apr-2004, 14:59
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by meet_raman
how do we pass two-dimentional integer array into functions and also how to return a 2-d integer array??

It is actually quite a bit different. Here is an example that passes a 2-d array by reference:
CPP / C++ / C Code:
#include <stdio.h>

void some_function(int a[10][10])
{
	int i,j;
	
	for(i=0;i<10;i++)
		for(j=0;j<10;j++)
			a[i][j] = i*10+j;
}

int main()
{
	int a[10][10];
	int i,j;
	
	some_function(a);
	
	for(i=0;i<10;i++)
		for(j=0;j<10;j++)
			printf("Value: %d\n",a[i][j]);
			
	return 0;
}
  #7  
Old 01-Apr-2004, 15:26
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Arrays are ALWAYS passed by reference (value). You do not need to make a pointer to an array. You can simply pass it like this:

CPP / C++ / C Code:
#define SIZE 5

void testfunc(int testArray[][SIZE])
{
  // whatever you do to the array in here
  // will reflect on the actual value of the
  // array. Since arrays are always passed
  // by value, you can modify the array
  // contents and not have to return
  // anything.
}

int main()
{
  int myArray[SIZE][SIZE];
  
  testfunc(myArray);

  return 0;
}
__________________
-Aaron
  #8  
Old 01-Apr-2004, 15:37
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
You are absolutely correct Aaron. I missed the forest for the trees again. My sample still holds, but the comments in it are misleading.

Actually, I rarely use arrays in anything that I do. Pointers are much easier to use and understand IMHO and are dynamic.

Thanks again, aaron
  #9  
Old 01-Apr-2004, 16:16
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Oh, yeah. The code you posted was fine. I just wanted to clarify it for you so he could understand it a little better.
__________________
-Aaron
  #10  
Old 02-Apr-2004, 01:17
meet_raman meet_raman is offline
Junior Member
 
Join Date: Mar 2004
Posts: 34
meet_raman is on a distinguished road

got it..


thx a LOT for the quick response guys...

if i keep on getting advise like this from u, i wish there will be some day when i will be advising u on something..!! HEHE..

amen!

thx a lot again.. it might be an easy thing for u, not for me though.
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.