C Program returning a string.

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

cheezitman2001
Posts: 16
Joined: Tue Aug 07, 2007 5:07 am UTC

C Program returning a string.

Postby cheezitman2001 » Mon Jan 07, 2008 8:29 pm UTC

I'm supposed to make a program that takes some arguments, does some math on them, and returns a string. Is that possible? Right now my code looks like

Code: Select all

int main(int argc, char *argv[]) {
      /* Code to convert arguments to numbers goes here */
      char *myString = calcBandwidth(intArray[0], intArray[1], intArray[2],
            doubleArray[0], doubleArray[1], doubleArray[2]);
      return *myString;
}

Would something like that work? And is there any way I can test the program to see what it returns? I'm running Vista, but have access to an XP machine if that would work better. I had it set up to print the string before returning it, and that worked fine, so I just want to make sure that I'm returning it the right way, and that there's not a better way to do it. I just taught myself C a few weeks ago, and am still getting the hang of it.

Thank you very much.

User avatar
Hangar
Posts: 171
Joined: Fri Nov 23, 2007 3:41 am UTC

Re: C Program returning a string.

Postby Hangar » Mon Jan 07, 2008 8:58 pm UTC

Can you post the wording in the assignment? You can't return a string from a C program. They either want you to A) write a function which returns a string, B) write a program which prints a string to stdout or C) write a program which writes a string to a file. Each is pretty easy to do. It looks like you have A) done. To write to stdout, use printf:

Code: Select all

#include <stdio.h>

int main()
{
    char *myString = ...;
    printf(myString);
    return 0;
}


Writing to a file is only slightly more complicated:

Code: Select all

#include <stdio.h>

int main()
{
    char *myString = ...;
    FILE *file = fopen("somefile.txt", "wb");
    if (file == 0) {
        printf("Error, could not open somefile.txt");
        return 1;
    }
    if (fprintf(file, myString) < 0) {
        printf("Error, could not write to somefile.txt");
        fclose(file);
        return 1;
    }
    fclose(file);
    return 0;
}

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: C Program returning a string.

Postby segmentation fault » Mon Jan 07, 2008 10:16 pm UTC

if your main returns the char *, it would be returning the address as a number (i would guess, and taking into consideration that main returns a char * if it can even do that).

whats going on here can be 2 things:

1) your teacher wants you to write a function returning a char *
2) your teacher wants you to somehow write the string to a file or pipe it to some other process

im guessing 1. if you HAVE to write a function that returns a char *, go ahead. under normal circumstances however id advise passing a char *, modifying it within the function, and returning an int (success/fail). thats the proper way to do things.
people are like LDL cholesterol for the internet

cheezitman2001
Posts: 16
Joined: Tue Aug 07, 2007 5:07 am UTC

Re: C Program returning a string.

Postby cheezitman2001 » Tue Jan 08, 2008 12:38 am UTC

Thank you very much for the help. I misread the assignment. It was just a function that returned a string; you were right. I got it now.

Workaphobia
Posts: 121
Joined: Thu Jan 25, 2007 12:21 am UTC

Re: C Program returning a string.

Postby Workaphobia » Tue Jan 08, 2008 2:33 am UTC

segmentation fault wrote:if your main returns the char *, it would be returning the address as a number (i would guess, and taking into consideration that main returns a char * if it can even do that).

Main has to return an integer, if it returns a value at all. In C the "int" return type part of the declaration is optional, but good form. Void main is not legal, but supported by some compilers nonetheless. You shouldn't be able to declare main to return a char*, but there's no reason why you can't convert a value to an integer (even implicitly) and return it. In this case, it looks like his code returns the ascii value of the first character of the string.

cheezitman2001: Glad you got your assignment working. For future reference, typically when processes (programs) are run, they return an integer to the environment that executed it. This is true in unix and windows; I'd be interested to know of any widespread general purpose system that wouldn't do things that way. In C and many other languages, this is done via the return value of the main function. Main can't return a string because there's no concept in the operating system of passing strings back from the system calls that are used to execute programs.
Evidently, the key to understanding recursion is to begin by understanding recursion.

The rest is easy.

aib
Posts: 9
Joined: Wed Jan 02, 2008 11:42 am UTC

Re: C Program returning a string.

Postby aib » Fri Jan 11, 2008 4:35 pm UTC

Workaphobia wrote:
segmentation fault wrote:if your main returns the char *, it would be returning the address as a number (i would guess, and taking into consideration that main returns a char * if it can even do that).

Main has to return an integer, if it returns a value at all. In C the "int" return type part of the declaration is optional, but good form. Void main is not legal, but supported by some compilers nonetheless. You shouldn't be able to declare main to return a char*, but there's no reason why you can't convert a value to an integer (even implicitly) and return it. In this case, it looks like his code returns the ascii value of the first character of the string.


main() has to have the return type of 'int', or a compatible type as per §5.1.2.2.1 of the C99 standard (same in ANSI-C). You cannot omit the return statement either; though C++ allows this only for the case of main(), where an implicit 'return 0;' will be inserted at the end of the function.

I also thought you had to specify the return type AND a list of parameters when defining the function (i.e. "main(void) { ... }" and "int main() { ... }" were illegal) but the standard confused me further on the subject. There are some examples which have "int main() { ... }" in code.
Functions without prototypres are assumed to have a return type of 'int'; I'm not sure if this is in the standard, but it's not good practice to rely on this behavior.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests