8 Replies - 885 Views - Last Post: 07 June 2010 - 09:47 AM Rate Topic: -----

#1 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Help with non-integer function..

Posted 06 June 2010 - 08:33 AM

There are a few questions which I want to ask.

First
I'm trying to create a non-integer user defined function which returns a string inputted by user. What's the problem with this code?

#include <stdio.h>
int main()
{
char x[3];
x = retstr();
printf("%s" , x);
}

char retstr()
{
char y[3];
scanf("%s" , &y);
return(y);
}


I can't really explain the errors I'm getting(4 errors, 3 warnings - it says at the bottom).

Second
I don't know the proper process of debugging in Visual C++ 6.0(Should I get VS2010 Express?). Can you provide a link or something to explain that(interface of the program, what's what).

Third
Is it okay to start C++ or should I wait and learn C first?

Is This A Good Question/Topic? 0
  • +

Replies To: Help with non-integer function..

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Help with non-integer function..

Posted 06 June 2010 - 08:44 AM

well, the function is declared as returning a char, but you attempt to return a char[3].
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with non-integer function..

Posted 06 June 2010 - 09:15 AM

Quote

What's the problem with this code?
There's three problems in your approach.

One, you think copying arrays can be done with the = operator. It cannot. Do you see where you depend on copying with =?

Two, a C string is an array of characters. Not a single char.

Three, returning an array is a problem. Because when you refer to an array, syntactically it decays to a pointer, you can't return an array. You also end up getting a pointer. Where does it point? To an array. The problem is that this array was allocated on the stack in function retstr. What happens when function retstr returns? That array is deallocated. In other words, you end up returning a pointer to a deallocated array. Obviously this is not good.

There are two approaches one can take. Dynamically allocate the array and return a pointer to that array. Or, pass in an array, (which decays to a pointer to that array).

You should generally take the second approach.

Quote

I can't really explain the errors I'm getting(4 errors, 3 warnings - it says at the bottom).
You can't explain them. But you can copy paste them here. Right?

Quote

I don't know the proper process of debugging in Visual C++ 6.0(Should I get VS2010 Express?).
You should get VS2010. If you want to learn to use the debugger, Google and read up on tutorials. If you want to learn as a programmer how to conceptually debug, practice. You learn by practice.

Quote

Is it okay to start C++ or should I wait and learn C first?
If you want to learn C++, learn C++. Not C. If you want to learn C, learn C. They are two separate languages.
Was This Post Helpful? 2
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: Help with non-integer function..

Posted 06 June 2010 - 09:58 AM

View PostNikhil_07n, on 06 June 2010 - 07:33 AM, said:

First
I'm trying to create a non-integer user defined function which returns a string inputted by user. What's the problem with this code?

#include <stdio.h>
int main()
{
char x[3];
x = retstr(); 
your variable name x represents an identifier for an array; When you perform 'assignment' (i.e. the default behaviour of the = operator), you are performing an automatic copying operation whereby the data within some memory is copied to another location. C and C++ do not support automatic copying of arrays (i.e. you cannot pass or return arrays "by value") - this is the reason why your compiler is complaining.

In general, returning an array from a function is a really bad idea - a better way is to pass an array to a function (i.e - make sure that the array has been created before the function is called). Since arrays cannot be copied, your program will not pass "by value" (which is the default behaviour for other types) - instead, the compiler will do a few things behind the scenes, and the function will recieve a pointer-to the first element of the array, which in turn gives your function access to the entire array which is adjacent in memory; effectively meaning that arrays are always passed by-reference to a function. (in C++ 'by pointer' and 'by reference' are two different things, so that's not 100% accurate, but in the example below, the outcome is the same)
#include <stdio.h>
int main()
{
    char x[3];
    retstr(x);
    printf("%s" , x);
}

void retstr(char y[])
{
    scanf("%s" , &y);
} 
note that the following signatures are all identical as far as the compiler is concerned

void retstr(char* y)
void retstr(char y[])
void retstr(char y[3])
void retstr(char y[1000])

in every case, the data type of the variable/identifier y as far as the retstr() function is concerned is always char*

Have a read here for some more in-depth info on arrays and how the compiler works its behind-the-scenes pointer magic to make life easier: http://c-faq.com/aryptr/index.html

View PostNikhil_07n, on 06 June 2010 - 07:33 AM, said:

Second
I don't know the proper process of debugging in Visual C++ 6.0(Should I get VS2010 Express?). Can you provide a link or something to explain that(interface of the program, what's what).
Visual C++ 6.0 is ancient, you could get VS2010 express, or any other modern free IDE/compiler (Code::Blocks is a pretty good IDE and comes with the MinGW compiler - there are also many others which you can find if you search google). there's no good reason whatsoever, as a novice just starting out, to disadvantage yourself so heavily by using a compiler which pre-dates standard C++, and which most people don't use much any more except for maintainance of old/legacy code. the creation of the C++ standard in 1998 involved massive changes to the language - the main disadvantage from your point of view is that newer books, and many websites will offer code examples and tools which simply won't work in VC++6.0

I believe the MSDN website has a pretty good article explaining how to use the VS2010 debugger. it actually does quite alot of things which you'll probably not need - although probably the most useful feature for you at the moment will be the ability to 'step over' code as its running.


View PostNikhil_07n, on 06 June 2010 - 07:33 AM, said:

Third
Is it okay to start C++ or should I wait and learn C first?
I think this short FAQ entry perfectly sums up the answer: http://www.parashift...p.html#faq-28.2

C++ FAQ Lite said:

Don't bother.

If your ultimate goal is to learn OO/C++ and you don't already know C, reading books or taking courses in C will not only waste your time, but it will teach you a bunch of things that you'll explicitly have to un-learn when you finally get back on track and learn OO/C++ (e.g., malloc(), printf(), unnecessary use of switch statements, error-code exception handling, unnecessary use of #define macros, etc.).

If you want to learn OO/C++, learn OO/C++. Taking time out to learn C will waste your time and confuse you.
The C++ FAQ Lite is a great starting point for tips and advice if you're just starting out - the rest of that page might interest you, too.

This post has been edited by Bench: 06 June 2010 - 10:06 AM

Was This Post Helpful? 4
  • +
  • -

#5 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Help with non-integer function..

Posted 06 June 2010 - 01:19 PM

An IDE is a great tool to work with, especially when projects contain more than one file.
When it's only a file, i prefer to use a texteditor and compile via command line, because it's more fast.
Was This Post Helpful? 1
  • +
  • -

#6 GrammarNazi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 06-June 10

Re: Help with non-integer function..

Posted 06 June 2010 - 01:21 PM

Quote

because it's more fast


It's faster.
Was This Post Helpful? 1
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Help with non-integer function..

Posted 06 June 2010 - 02:09 PM

Good to know that we have a translator.
Was This Post Helpful? 0
  • +
  • -

#8 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Help with non-integer function..

Posted 07 June 2010 - 06:04 AM

View PostGrammarNazi, on 06 June 2010 - 08:21 PM, said:

Quote

because it's more fast


It's faster.


Made a typo. Anyway thanks for the correction.
Was This Post Helpful? 0
  • +
  • -

#9 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Re: Help with non-integer function..

Posted 07 June 2010 - 09:47 AM

Since I don't have any knowledge of Pointers, I didn't get most of the things you said. But I'll look at this thread again when I start pointers.

And thanks for clearing the other two doubts, really appreciate that. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1