C programming help

GargGarg Purveyor of Lincoln Nightmares
edited April 2008 in Science & Tech
Guys, I hate it when people ask for help on their homework here, but that's exactly what I'm about to do. I've run out of time :(

So I've got this really simple exercise that I'm struggling with because
- I don't really understand how character arrays work
- I don't know if I'm calling a function in another .c file correctly

We've been provided a skeleton driver file that is supposed to send strings to functions written in another file. Those functions are supposed to encrypt the strings based on simple rules. I'm trying to get the first function working in the if block, below.
My current error is "20: warning: passing argument 2 of 'strcpy; makes pointer from integer without a cast"

I'm trying strcopy because when I was trying to use direct assignment (output = simpleE(input);), I was getting "incompatible types in assignment."

[php]#include <stdio.h>
#include <string.h>

/* encryption */
int main ()
{
char encryption;
char input[36], output[36]; /* More than enough for 20 characters */

printf("Please choose an encryption level: ");
scanf("%c", &encryption);

printf("Please provide an input sentence:\n");
scanf("%s", input);

if (encryption == 's' ) {
/* Call simple encryption function, please fill in */

strcpy(output, simpleE(input));

} else if (encryption == 'm' ) {
/* Call medium encryption function, please fill in */
} else {
/* Call ultra encryption function, please fill in */
}

printf("Your encrypted sentence is:\n %s", output);

return 0;
}[/php]The simpleE function is in another .c file that's been compiled. I've been experimenting with how the function is defined because I'm having trouble wrapping my head around how character arrays work, so my function definition could be really screwy.

[php]#include <stdio.h>

/* encryption implementation*/
char* simpleE(char *input)
{
char rstring[36];
int i = 0;

for(i = 0; i < 36; i++)
{
rstring = input + 1;
}

return rstring[36];
}[/php]

Basically, if anyone can tell me how I should properly pass a character array to another function and return a character array from it, that'd be of great help. Thanks :). This stuff is making me feel like an idiot. I considered myself pretty decent at VB, but the syntax is killing me in C.
</stdio.h></string.h></stdio.h>

Comments

  • JBJB Carlsbad, CA
    edited April 2008
    why are you returning "rstring[36]" in simpleE?

    a) that notation dereferences the 37th element
    b) your array has 36 elements
  • JBJB Carlsbad, CA
    edited April 2008
    second question:

    Why not make output a character pointer, since simpleE is already allocating the memory and returning a valid pointer (assume you fix the bug from my first post ;))

    Speaking of memory allocation, you may want to revisit your allocation strategy in simpleE. If you want to return a pointer to data you need to make sure the data is still valid once you leave the scope of the function.
  • GargGarg Purveyor of Lincoln Nightmares
    edited April 2008
    JB wrote:
    why are you returning "rstring[36]" in simpleE?

    a) that notation dereferences the 37th element
    b) your array has 36 elements

    You're right, it shouldn't be returning a specific element like that.
    JB wrote:
    second question:

    Why not make output a character pointer, since simpleE is already allocating the memory and returning a valid pointer (assume you fix the bug from my first post ;))

    That makes sense. I think I have a workaround where I send one character at a time to get encrypted, but if I could get the arrays/pointers to work , that would be more elegant.

    Thanks for the tips, JB! I don't get to sleep tonight until this is done :D
  • JBJB Carlsbad, CA
    edited April 2008
    no problem. Why is simpleE making a copy? Can't you perform the encryption in place on the "input" variable?
  • mertesnmertesn I am Bobby Miller Yukon, OK
    edited April 2008
    Welcome to programming, Gargoyle. It's the land of sleepless nights and coding daymares.

    It's been a very long time since I've done anything with C/C++, but going from your compiler error it looks like you might want to try casting the second parameter in your strcpy statement as a string or character.
    Can't remember how to do it off the top of my head, but I could do it in Ada... ;)
  • GargGarg Purveyor of Lincoln Nightmares
    edited April 2008
    Thanks for the help, guys!

    Man, I was stressed out last night. I thought it was due at midnight, and I would have failed the class if I didn't get something turned in. Turned out it was due today at midnight, and I just got it done.

    I think I understand how pointers and character arrays work now (especially after going to one more lecture), but I largely avoided the problem by passing one char at a time. I also figured out how to call external functions correctly.

    It was all simple stuff, but the textbook was just confusing me more. Learning C from the "classic" Kernighan & Ritchie book is like learning English by reading Shakespeare. We've come so far since when it was written that something a little more modern would be helpful. Something like Icrontic :D
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI
    edited April 2008
    DO YOUR OWN WORK, NUB
Sign In or Register to comment.