Re: Cannot return values of char variable




Pedro Pinto wrote:

Hi there once more........

Instead of showing all the code my problem is simple.

OK - so give us a simple testcase.

I've tried to create this function:

char temp(char *string){

alterString(string);
return string;
}

char alterString(char *string){}

temp receives a string and uses the alterString to modify it.

OK - so write a minimal testcase which uses these functions to work
with a string, show us the whole code (cut and paste, don't retype),
tell us what input you gave it, what output you expected and what
output you got. Then we've got something to work on.

Here's the sort of thing I mean (this is just an example, which I
didn't expect to work) :-

#include <stdlib.h>
#include <stdio.h>

char alterString(char *string){}

char temp(char *string){

alterString(string);
return string;
}

int main(void) {

char myString[]="Hello Pedro";
printf("I started with [%s]\n",myString);

printf("I got [%s]\n",temp(myString));

return EXIT_SUCCESS;
}


When I compiled it, I had this warning from gcc:-
pedro.c: In function `temp':
pedro.c:9: warning: return makes integer from pointer without a
cast

When I ran it, this happened:-
./pedro
I started with [Hello Pedro]
Segmentation fault

Which is fair - temp() returns a char, not a char *, but I told printf
to treat it as the address of (the first of) a null-terminated set of
characters.

I think part of your problem is that temp() and alterString() should be
declared as returning char *, not char, but beyond that - who knows?

.



Relevant Pages

  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: [OT] My First C# (warning - long post)
    ... cut me some slack - show me a COBOL program with less than 15 working-storage variables! ... Yes, the Trimis probably extra now, but that was my attempt to get it to quit griping at me that I had given it a "String", when it wanted a "char". ... public string IBreturn ...
    (comp.lang.cobol)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)