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!

Passing arrays to functions 3

Status
Not open for further replies.

Greenleaf

Programmer
Feb 14, 2002
93
IT
Hello,
seems like I have a problem in passing an array to a function and then passing the value back to the main program.
This program reads a sentence and converts the first letter in a capital letter, if needed, and converts the rest in non capital.

Code:
#define maxarray 40

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

char change(char word[]);

void main()
{
	char word[maxarray];
	printf(&quot;\nIntroduce sentence  &quot;);
	gets(word);
	word=change(word);
        printf(&quot;\nThe sentence is:  %s&quot;, word);

}

char change(char word[])
{
      int i;
      if (( 97 <= word[0]) && (word[0] <= 122 ))
	  word[0] = word[0]-32;
	for (i=1; i< maxarray; ++i)
	  {
	     if (( 65 <= word[i]) && (word[i] <= 90))
	       word[i]= word[i]+32;
	  }
       return word;	
}

I can't pass the word back to main. It signals an error.
I could as well make the function print the final sentence but that's not what I want.
Can anyone help me?



 
Rob, your question is a bit confusing. Correct me if I have understood it wrong.

Point 1: U want to have a pointer in your main() where you'll get the input from the user.
Rem 1: Well, this isn't the scenario where you should use a pointer. B'coz, you should know the lenght of the input the user might enter! So, what u need to do is to take the input in a buffer (a regular char array with exceptionally large size to accomodate the maximum length expected) and then do something like
Code:
org_str=(char *) malloc (strlen(buffer)*sizeof(char));
(assuming org_str to be a char *)
and then passit to the function.

Point 2: U think, u'll need to allocate memory for the pointer argument.
Rem 2: That'll destroy the reference to the memory it was having earlier.
Note: change (char * str) and change (char str[]) are ONE AND THE SAME. No difference at all. The argument str holds the reference of the array passed in to the function, it already has a memory allocated with data in it. No further allocation is required.

Try focusing on the essence of Point 2, it'll answer your question.;-) (BTW, am thru with my office work and am leaving home, I'll be back tomorrow.. till then bye!)

Roy.
user.gif
 
This works fine on my puter.
As the man said pass the pointer to the array
and dont wory about returning a pointer or
whatever. keep it simple.

please not the changes in the function call and
the function definition.

#define maxarray 40

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

void change(char *word);

void main()
{
char word[maxarray];
printf(&quot;\nIntroduce sentence &quot;);
gets(word);
change(word);
printf(&quot;\nThe sentence is: %s&quot;, word);

}

void change(char *word)
{
int i;
if (( 97 <= word[0]) && (word[0] <= 122 ))
word[0] = word[0]-32;
for (i=1; i< maxarray; ++i)
{
if (( 65 <= word) && (word <= 90))
word= word+32;
}
}



 
also consider using &quot;toupper ();&quot;
Just call toupper to change the value
of the char to upper case, Dont check it
just change it.
 
You can also use tolower(int c) to lower things.

(Yes, I know this was a pointless post, but I've been asking so many question I felt like I needed to answer one, even if its one that didn't exist.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top