?
Solved

Function String return type instead of Void

Posted on 2003-03-24
15
Medium Priority
?
401 Views
Last Modified: 2010-04-15
Hello. I have this function that successfully populates the address space of the variable passed in with a unique value. The problem is I would like the function to not be of return type void, and would instead like it to return the value of the variable pw, so I can assign a string variable to that pw. Can anyone help me out?

void hash_pw( char * key, int size, char * pw ) {

     unsigned long n, q;

     n=0;

     for ( q=0; q<strlen(key); q++ ) {
             n = n*1103515245+ (long)key[q];
     }

     for ( q=0; q<size; q++ ) {
             if (n%36 > 9 ) {
                     *(pw+q) = (char) (n%36+87);
             }
             else {
                     *(pw+q) = (char) (n%36+48);
             }

             n = n*1103515245+12345;
             if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;

     }

     *(pw+size) = (char)0;
     return;
}


0
Comment
Question by:dynomutt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 75 total points
ID: 8196039
I think all you would need would be:

char *hash_pw( char * key, int size, char * pw ) {


and then at the end:

return(pw);
0
 

Author Comment

by:dynomutt
ID: 8196075
so then I would put in the main block:

pwval = hash_pw(argv[1], size, pwval);

?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8196675
Yes, but who cares?  Why not just a void return?

The string is already modified, and actually assigning pwval is not going to change anything.

In other words,

pwval = hash_pw(argv[1], size, pwval);

is identical to

hash_pw(argv[1], size, pwval);

However, neither will let you copy the string to a new variable.  You cannot do this:

char pwval[100];
char newpwval[100];

newpwval = hash_pw(argv[1], size, pwval);

This won't work - the contents of the string cannot be copied in C by using =.

This is like the strcpy() function.  It returns the address of the destination string, but nobody ever says:

dest = strcpy(dest,source);

you just say

strcpy(dest,source);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:mirko_germany
ID: 8198635
pw isn't a variable, but a pointer. this means, that you write to an address in the ram. but this address doesn't change at leaving the function. so if you've declared a function by

void function(char *pointer)
{
   //...
}

you can call it by

char *pointer;
function (pointer); // or function (&pointer);

and then you can use it in your main program:

printf("%10s\n", pointer);

mfg
mirko
0
 
LVL 6

Expert Comment

by:gj62
ID: 8198722
"pw isn't a variable, but a pointer."

Um, pw *is* a variable, that happens to be a pointer...

char * pw;
char * pwCopy;

char * function (char * p)
{
  return(p);
}

pwCopy = function(pw);

Now, you have 2 variables, pw and pwCopy.  They are DIFFERENT variables, stored in different places in memory.  They are both pointers.  They both point to the SAME place.  If you modify the string pointed to by pw, then access it through pwCopy, the change is still there because it is pointing to the same location in memory...



0
 

Expert Comment

by:mirko_germany
ID: 8198791
"pw *is* a variable"

that depends on your definition of variable: to me, the value of a variable is a number, a character or a string of numbers or characters. but the value of a pointer is an address. and in "char *pw" there is only the address (e. g. 0xDF3A7AE0), but in "char pw" a letter (e. g. 'a') is saved in.

but this defintions don't solve the problem. refering to the problem, i think, both possiblities (yours and mine) would work. @dynomutt: just try!
0
 
LVL 6

Expert Comment

by:gj62
ID: 8198841
Mirko -

If you want to redefine the idea of a variable, fine.  I'll stick to what the ANSI standard and K&R and the rest of the C communicty define as a variable.  

If a pointer isn't a variable, how come you can change it's contents?

char *pw;

char string[]="THIS IS MY STRING";

pw = string;
printf("%s", pw): /* this will print THIS IS MY STRING */

pw += 5;
printf("%s", pw): /* this will now print IS MY STRING */

Notice that the memory where THIS IS MY STRING is stored wasn't changed - the only thing that was changed is the value of pw - a pointer variable.

It might be easier for you if you think of pointer as a type.

e.g. the following are all TYPES of VARIABLES

int
long
* int /*a variable that is a pointer to an int*/
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8199613
mirko:
why is it hard to accept what gj62 said?
pointer is just another "type of variable".  btw its not his personal definition, its the standard

and as for my personal interpretation a pointer is as good as an integer(may be long), i find it very hard to differentiate integer from pointers.

0
 

Expert Comment

by:mirko_germany
ID: 8219356
i don't believe that our discussion of pointers and variables helps dynomutt to solve the problem - you have your opinion, i have mine. exit(0).

and now refering to the problem:

i read your program code again and realized the following:

you defined your function with void hash_pw(...){...}. how should this function return a value. void means: no value returned! use type *char or something like that, but not void. i wonder why your compiler not said: "function void cannot return a value", when seeing the return-command. so do it that way, or use pointers which aren't deleted while leaving the function.

mirko
0
 
LVL 6

Expert Comment

by:gj62
ID: 8221315
Mirko,

The posted code *doesn't* return a value - it simply returns, which is perfectly legal for a void function.

There's a difference between differing on implementation, versus differing on the definition of something.  The entire C community refers to pointers whose values can change as a type of variable.  Your refusal to adopt that definition is not an opinion, any more than saying that in your opinion, the natural numeric order is 1, 3, 2, etc...

Now, you are certainly entitled to THINK about the problem however you wish, but if you want to help someone, you will need to accept the common language and definitions used (in this case, the C standard), or else you really should not be using this forum.  

I understand that you yourself are beginning to learn the language in depth.  Great!  Hopefully this dicsussion will help you too...
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8221981
>>>in your opinion, the natural numeric order is 1, 3, 2, etc...

nice idea ..i was getting bored to count always in the 'natural order' 1 2 3 ...
0
 

Expert Comment

by:mirko_germany
ID: 8226775
"The entire C community refers to pointers whose values can change as a type of variable.  Your refusal to adopt that definition is not an opinion, any more than saying that in your opinion, the natural numeric order is 1, 3, 2, etc..." is my english such bad, that you didn't understand what i said??? "you have your opinion, i have mine. exit(0)." i don't want to discuss about it any longer, because it doesn't help! he wants to write a program and not a referat about c!

"it simply returns":
void test()
{
     return 0;
}

void main()
{
}

compiler-answer: "'test()' cannot return a value"
help: "(Funktion vom Typ void kann keinen Wert zurückliefern)

(Die Kommandozeilenoption zur Anzeige dieser Warnung lautet = )

Eine Funktion mit Rückgabetyp void enthält eine return-Anweisung, die einen Wert zurückliefert, beispielsweise einen int."

in english: "(function of type void can't return a value)

the commandlineoption to show this warning is =

a function with a return-value-type void contents a return-command, which returns a value, e. g. an int." that isn't possible!!! believe me and borland!

mirko
0
 

Author Comment

by:dynomutt
ID: 8262200
This first answer actually gave me the results I was looking for...but then I did realize what one of you has already pointed out...that it doesn't matter really. Since the void function still modifies the data that pointer *pw points to, whatever is passed in as the third argument, a pointer argument, that is what I would need to work with. It doesn't necessarily need to be returned in order to work with it. Actually the main issue was that I was using RPG on IBM AS/400 systems...and they have modules that allow you to call C programs...but we were having trouble getting the corret value calculated in the C program to return correctly to the AS/400 RPG program. For some reason I felt that to do what we needed I had to have that function RETURN a value, and then the RPG program would call that funtion via a variable assignment. But then we realized that it should still work even if the function is void, as long as the RPG program passes in a pointer, then the C program should be modifying data in an address space that the RPG program can see. I hope I made some sense, sorry to take so long to respond to this posting. Thanks for all of your input/feedback.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8265977
mirko:
>>"you have your opinion, i have mine. exit(0)."

u called exit(0) .. not us, so we kept executing .. but u still came back with another execution of urs.
:)
take it as nothing more than joke ..

now my exit() :)
0
 

Expert Comment

by:mirko_germany
ID: 8270177
su root kill akshayxx
;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question