The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

VC++6.0 : Getting char to accept char[17]

I've got a variable sn as follows :

char sn[17];

And I assign it a value.

I then have another char function tx as follows :

char ap[17];
char tx (ap)
{
return ap;
}

Why do I get the message : cannot assign char[17] to char ?

How do I get the function tx to accept ap ? Please help!
Ezani Send private email
Wednesday, June 06, 2007
 
 
char ap[17] is char* not char. Duh? Are you just starting to learn C?
Donald Duck
Wednesday, June 06, 2007
 
 
Exactly.  A 'char' is an 8-bit byte (usually).  A char[17] is 17 bytes, from char[0] through char[16].  And 'C' usually assumes you use one of those bytes for a string-ending 'null' byte.  Well, the 'strlib.h' library does, anyway.

So it looks like you want to 'return' a 'string', right?  Well, what you have to realize then is that 'return' can return one value.  This value can be a long, a char, a pointer, or a reference, but it's still one value.

If it was a C++ String type, I believe you could do this, since what you'd return would be the address-of the string, or a reference-to the string.

Now, you COULD return the address of 'ad', but then you'd be returning a "char *", not a "char".
AllanL5
Wednesday, June 06, 2007
 
 
you "assign it a value?" it is a buffer, you can copy a string into it, don't say "assign it a value."
onanon
Wednesday, June 06, 2007
 
 
Hard to figure out what you're doing.

char sn[17];

char tx(char ap)
{
return ap;
}

The above will return the char that is passed in, but I don't think that's what you're after.

Are you trying to do this:

char sn[17];

char* tx()
{
  reutrn sn;
}

??

I wouldn't recommend that either (caller of tx has no idea how big the buffer is).

Personally, I'd go for:

std::string sn;

std::string tx()
{
  return sn;
}

with caveats about how evil global variables can be, assumptions about single-threadedness, etc, etc, etc.
Doug
Wednesday, June 06, 2007
 
 
Sounds like you really need a good intro/tutorial C++ book that starts at the beginning (i.e., doesn't assume you already know C, because you clearly don't).

Don't forget to do all the exercises...

See you in six months...
BillT Send private email
Wednesday, June 06, 2007
 
 

Wednesday, June 06, 2007
 
 
[http://sunpacplus.netfirms.com]

You'd notice that I used the VBScript menus instead of the JavaScript so there's no layers. The rest of the text had to be pushed down so the menu dropdowns had the space! I'm planning to move to a JavaScript menu which should have none of those problems.

Ok back to my OP. My fundamental error was stating that a char can be assigned to a char[17] which it cannot. There are 17 char in char[17], lets put it that way. So I would better use an index as such char = sn[idx] and loop through or use char*.

I've got 26-years in VB and I am a C minimimalist - just touching Visual C++ off and on (I can count the number of times!). When I was young I bought Borland Turbo C 1.5, but got turned off when I couldn't get a result for 4/3 as in BASIC at that time. Turned out I had to do this :

float mycalc()
{return 4/3;}

The sensitivity to case, forgetting the semicolon and the curly brackets was a real headache. And then with the concept of pointers and address of, that made it even more complicated. And then the bubble burst with the introduction of C++ and its polymorphism, operator overloading, templates and ... the code got so complicated I couldn't take it anymore...
Ezani Send private email
Wednesday, June 06, 2007
 
 
You think that's bad?  5 minutes with Haskell will have the maid cleaning burnt brains out of your carpet for the next month.

C++ isn't the same as Visual Basic?    Say it ain't so!

Thursday, June 07, 2007
 
 
Ezani, if you are interested in learning this kind of first-principles stuff (and I do highly recommend it), then I suggest reading through "The C Programming Language" by Kernighan and Richie. That's pretty much *the* book for learning C, and it'll take you through all these things and show you what they all represent "under the hood". And I personally think that can only help you when you're working in higher-level languages like C++, C#, etc.
Jesse Send private email
Friday, June 08, 2007
 
 
That's the first book I have read Jesse and believe me when they explain pointers and addresses in separate chapters in simple console type examples, it appears simple..but try programming large scale enterprise application C++, MRP or ERP-style...

Compared to other languages (VB), in C/C++ you have the added complexities of header files and then all the #includes at the top, then the #DEFINE, #CONSTANT, etc...

I thought I got this right to calculate 1.3333333333333 :

float calculateOne()
{
return (4/3);
}

but even such a simple program in C can return a long answer.

What I mean is when you add standard templates and structures and database access, etc in large scale enterprise apps, C/C++ CAN be intimidating even for a 26-year VB6 programmer like me (from a purely structural progamming point of view).  Especially the --> and the * and the &. I really don't know when and how to use them instead of purely using the variable name itself. It's so much easier in VB6...

mystring$ = "Please say you can do it."

But in C/C++:

char *mystring = "Please say you can do it."

And the strings are also stored differently:

VB : 25Pleasesayyoucandoit.
C/C++ : 25Pleasesayyoucandoit.0


But having said that, if Windows itself is written in C/C++, then it would be probably better to use the language as its interacting more at the machine level and you have the asm keyword. But looking at recent trends, the power of C/C++ is going..going..gone with the inclusion of memory management and auto garbage collection and intermediate languages (read MSIL) and just-in-time compiling.
Ezani Send private email
Sunday, June 10, 2007
 
 
Ezani, you should know they've been predicting the demise of C since at least 1980.  Hasn't happened yet.
AllanL5
Monday, June 11, 2007
 
 
I know, Allen but I think C/C++ is "under attack" now with Java, C# and Visual C++ 2005. With memory management, we are losing the power of actually being able to control and access the computer's memory. With intermediate languages, the speed and native compile of C/C++ is gone. A lot of embedded device and lowdown programming used to be done in C/C++ but it seems Java is taking over. I reminisce the old days when Borland was going strong and taking Microsoft head on. At least we had two solid products C++ Builder and Visual C/C++.
Ezani Send private email
Monday, June 11, 2007
 
 
you know how you are confused as to when you should use the variable name and when you should throw some symbols in there?  That's because the compiler is aware of a distinction that you are not.  Those distinctions give you expressive power.  That is why C++ is not going away in the next 100 years.

I recommend staying away from C for a while and focusing on C++.  Good C++ code almost never uses arrays.  It also rarely uses the new keyword, as opposed to good java code.  You might want to try "Accelerated C++".  It is inexpensive and good for people new to C++.  Then I would follow up with "The C++ Programming Language".
Tom C
Tuesday, June 12, 2007
 
 
Tom, you exactly hit the nail on the head with :

[you know how you are confused as to when you should use the variable name and when you should throw some symbols in there]

I really need to know when. If somebody can give me some clear to understand basic rules on the above, I think I will be able to nail C/C++ ?

What are the basic rules for when you should use:

string abc = "hello;

abc[] = "hello";

abc[0] = 'h';abc[0] = 'e';abc[0] = 'l';abc[0] = 'l';abc[0] = 'o';abc[0] = '\n';

char *abc = "hello";

And how would I assign hello to an abc variable by using & (address of) ?

Given the assignment above, how many ways can I then get back "hello" with :

(1) basic string abstractions at the highest level (string or __STRING)
(2) pointers (*)
(3) addresses (&)
(4) arrays ([]) ???
Ezani Send private email
Tuesday, June 12, 2007
 
 
Hi Ezani

The following functions in C++ will basically do the same thing, but slightly differently.

string abc = "hello";

This requires the <iostream> libraries and "using namespace std" as well. the 'string' variable is 'extra' in C++ - it's not 'as' standard as say int and char are. This line would create a string variable, and assign it 'hello' and you would manage it using the std::string library functions.

abc[] = "hello";

This will create an error. Inserting 'char' before 'abc' will create a character array containing 'hello'. The last character would probably be automatically set as '\0' (end of character stream), but I'm not sure. If you wish to set an entire array's values using C++, it is 'better' to do this when you are initializing the variable - like here.

abc[0] = 'h';abc[1] = 'e';abc[2] = 'l';abc[3] = 'l';abc[4] = 'o';abc[5] = '\n';

This is perfectly fine for setting the values of a char[], and if you have already declared the array it is the only way to assign the array values. Don't forget that at the end of an character stream you should really have the '\0' character to signify the end of the character stream.

char *abc = "hello";

This creates a char pointer to a space in memory which contains the characters 'h' 'e' 'l' 'l' 'o' and '\0'. To dereference abc, you should use the * symbol: i.e.

std::cout << *abc;

which reads as

Output THE VALUE POINTED BY abc

Note: Try not to mix char[] and string. Use one or the other, but not both - mainly as it's bad programming practice, but also it could get confusing. One usually should be enough. The 'String' library is a 'workaround' for char[], and is not a 'standard' as such - well I don't think it is anyway. Try them both, see what you think.

I know I haven't fully answered your question, but this might be of some use hopefully. :)

Scott
Scott Davies Send private email
Wednesday, June 13, 2007
 
 
Ok, there are two issues here: value/pointer/reference semantics/syntax and C++ string types.

I don't know VB but I used to know java so I will use that to contrast C++.

In java you have two kinds of types: primatives and objects.  Primatives are things like int and char.  Objects are everything else, including all user defined types.  There is a very clear distinction between primatives and objects.

Primatives are about value.  Variables of primative type hold values.  The assignment operator copies the value so that the two variables now have the same value.  Primatives do not allocate memory.  If you have a local variable then the data is stored locally.  If you have a member variable then the data is stored in the object.

Objects are about identity.  They can't be put directly into variables.  Instead the variables hold what java folk call references.  The references refer to the object.  When you use the assignment operator you don't copy the object, you copy the reference to it.  New objects are made using the new operator which allocates the memory somewhere.

You can't have a reference to a primative and you can't operate directly on the value of an object without first going through a reference.


C++ is completely different.  The primative/object distinction almost does not exist.  Instead types like int and char are called built-in types.  Instances of built in types are considered objects too.  Your user-defined types are indistinguishable from built-in types with only one exception (that I can think of):  you can't inherit from a built-in type.

In C++ we have three ways to deal with our objects: value, reference, and pointer.

Value is just like value in java.  However it is not limited to built-in types.  By default all objects can be copied by value.

C++ pointers are a lot like java references.  However they don't automatically dereference to the object.  For example you have a point type and a point pointer:

Point origin = Point(0,0); //makes a point
Point* position = &origin; //make a pointer to origin

the variable origin contains a point.  The variable position contains a pointer to a point.  That pointer is a value that can be passed around.  If you want to access the point that it refers to you can use the * operator. 


The way that I like to think about pointers is in terms of relationships.  For example a URL is a pointer to a website.  A website is a website.  You can copy and paste the URL without affecting the underlying website.  You can store the URL in notepad.  Then when you want to access the website you pass it into a web browser which dereferences it.  Then you can operate on the website.  Languages like java do not make the distinction between the pointer and what is pointed at.  URLs and websites are very different things and C++ recognizes that concept.

C++ also has a third kind of variable, called the reference.  C++ references are nothing like java references.  Instead they are aliases.  References are very, very useful.  Many C programmers use pointers in situations where references would be superior.  Well, sorry I can't write more now.

As for strings go with std::string.  However you can't get away from character arrays completely.  This is because string literals are not strings!  This is because of the C legacy.  Generally you can be oblivious about it.
Tom C
Wednesday, June 13, 2007
 
 
The string template has probably built in the char array assignments (plus adding a \0 at the end of the array) within it so we can use it comfortably to assign strings in a more "natural" VB-like way rather than char mystr[0] = 'h', mystr[1] = 'e', mystr[2] = 'l', etc. But it still does the char assignments invisibly.

Then wouldn't it be right to say that C is such an optimal language because instead of assigning a whole bulk of string characters and passing it around as in VB and others, we merely manipulate the POINTER to the first string letter (C/C++ points to things) and also knowing the length of the string ? This is why C/C++ is such an optimal language..instead of passing a whole bunch of strings, it just passes the pointer to the first string character and also the length of the string thus speeding things up ?

Also, the use of the & (memory address) character allows C to just pass around hexadecimal memory addresses instead of the whole string thus resulting in very fast and small processing load ?
Ezani Send private email
Wednesday, June 13, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz