|
-
Aug 21st, 2000, 09:41 AM
#1
Thread Starter
Member
hi ppl,
i've been programming in vb for about 4 years now, but still have been puzzled by the "$". what does the "$" sign do? what is the difference between
asc$("A")
asc("A")
?
the other question i have is about pointers. in C there are pointers, but how do you define a pointer in VB? is there a pointer in VB?
Thanx, for your reply(s)
Zhang Tian Hao
Visual Studio Enterprise 6.0 SP 4
[email protected]
-
Aug 21st, 2000, 09:48 AM
#2
Hyperactive Member
$ stands for string
Right returns a variant, Right$ a string, etc.
Nope no pointers in VB, however, of course there's ByRef, which comes close to pointers :-)
-
Aug 21st, 2000, 10:56 AM
#3
Also, note that any function that returns a string instead of a variant is slightly faster.
-
Aug 21st, 2000, 11:19 AM
#4
Member
Pointers
Now the subject of pointers has reared its ugly head again I'll take this opportunity to ask "what is a pointer?" I've heard the term used several times - I have been trying to teach myself C++ (unsuccessfully). From my understanding a variable is just a user friendly name given to a part of memory available for the app to use. So how does a pointer differ?
Thanx in advance
Matt
-
Aug 21st, 2000, 12:17 PM
#5
A pointer is kind of like a variable but it doesn't store the actual value but the memory address that it is referring to.
Lets say you have the following code:
Code:
int i;
int *iptr;
i = 3;
iptr = &i;
First it declares an integer called i and then a pointer to an integer called iptr (the '*' sign tells the program that it is a pointer and not a regular variable).
Now you store the value 3 in variable i. The computer then stores this value some where in the memory lets say at address &HE60E84.
You don't assign regular values to a pointer but the address to something it is suppose to point (or refer to) at.
In C and C++ you use the ampursand (&) to get the address. So &i does not return the stored value 3 but the address to where that value is stored in memory. In this example 0xE60E84.
Now you could use the pointer to change the value stored in the variable i like this:
*iptr = 5;
The * sign in this case tells the program not to assign a new value to the pointer but to the memory address the pointer is reffering to.
In other words it says: Store the value 5 at address 0xE60E84. That is exactly what this statement is saying also:
i = 5;
So you see you can use the pointer like it was the variable it is referencing.
Here's an other example in VB:
Code:
Private Sub Main()
Dim i As Integer
i = 3
Call Add5(i)
Debug.Print i
End Sub
Private Sub Add5(x As Integer)
x = x + 5
End Sub
When you run the above program it would print 8 in the immediate window right.
That is because VB pass arguments by reference as default. So the x argument is the Add5 procedure is actually reffering to the local variable i in the Main procedure. x is in other words a pointer to i.
In C and C++ arguments to functions are always passed by value. So the same kind of program in C could look simular to this:
Code:
void main() {
int i = 3;
add5(i);
printf("%d", &i);
}
void add5(int x) {
x = x + 5; //or x += 5;
}
The problem with the above code is that it is adding 5 to the local variable x in the add5 function but the value stored in i is still 3 so the output of the main function would be 3.
In C you can't pass argument by reference directly but you can use pointers so you pass argument by reference indirectly.
So to write the above function so it works it would look like this:
Code:
void add5(int *x) { // x is declared as a pointer to an int
*x = *x + 5;
}
When you call this function you have to pass the memory address and not the value so the main function has to be written in this way:
Code:
void main() {
int i = 3;
add5(&i);
printf("%d", &i);
}
As you can see the call to add5 passes the memory address where the value 3 is stored (& = memory operator).
If you don't type: add5(&i) you would have passed the value 3 and the add5 function would have added 5 to whatever is stored on memory address 3.
You can imagine what would happen if the program is allowed to write to that address.
In the best case senario it would just overwrite an other variables value but in the most cases the program would crash.
I hope this didn't make you even more confused but rather clarified some thing for you.
Best regards
-
Aug 21st, 2000, 12:18 PM
#6
a Pointer Points to a variable(I am a C++ beginner, I think it can point to functions too, but i am not sure).
then you use the & Operator to get the addrss of the variable.
Code:
int* number;
//the the compiler its a pointer(with the *)
int Num1 = 77;
//give Num1 a value
number = &Num1;
//get the address of Num1
cout << *number << endl;
//that will output the value of Num1, which is 77
-
Aug 21st, 2000, 12:25 PM
#7
Yes you can let a pointer reference a function as well. A function name without parentheses returns the address of the function:
Code:
funkptr = myFunctionName
-
Aug 21st, 2000, 01:40 PM
#8
Monday Morning Lunatic
Function pointers are what you pass to the CreateDialog functions and others.
A pointer can also be manipulated like any other number 
Code:
char MyString[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char *p = myarray;
for(p; *p; p++) {
cout << p[0];
}
That just loops through a string by adding one to the pointer and checking if it equals NULL (end of the string).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 22nd, 2000, 04:11 AM
#9
Member
OK, thanx for the swift replies...but what advantage does using pointers have over standard variables? To me at a first glance it seems to do the same thing, just in a different way. In what scenarios is it beneficial to use pointers? Thanx again.
Matt
-
Aug 22nd, 2000, 06:02 AM
#10
The advantage is a pointers size. On a 32-bit environment an address is always 32-bit in size. That means that a pointer is always 4-byte large. Lets say that you want to pass a large string to a function. This string could actually be of any size and if you would pass this by value the program has to copy the whole string to the function argument.
But if you use a pointer you only pass 4 bytes (instead of maybe several kB).
-
Aug 22nd, 2000, 08:34 AM
#11
Hyperactive Member
But if you want to pass a large string to a function in VB,you can pass it ByRef. (Which is basically a pointer.)
If you ever did something like this:
Dim t As textBox
Set t = MyForm.Text1
From now on, t refers to Text1 on MyForm. If you set the Text of t (t.Text = "blah"), the text on the form is changed too. And when you type something in the textbox, the Text of var t is changed too. That's pretty much the same as working with a pointer (although with pointers you can have more fun, and can screw up a lot more :-))
Anyway untill M$ decides that VB programmers need pointers too (it took them a couple of versions to give us inheritance....), there's no real need to really understand pointers (at least not for use in VB), since it will only make you drool... ;-)
-
Aug 22nd, 2000, 09:04 AM
#12
This is true Crazy D but the reason for my explanation is that ca9mbu have tries to understand what pointers is and what they do. If you read the earlier posts this was actually a question about how C/C++ handles pointers.
-
Aug 22nd, 2000, 12:53 PM
#13
Monday Morning Lunatic
Global objects which need to be initialised in their constructor with function-specific information...
Like this:
Code:
MyClass* theC;
int WinMain(HINSTANCE hInstance, ...) {
theC = new MyClass(hInstance);
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 22nd, 2000, 01:21 PM
#14
Addicted Member
Back to the orginal subject, here is what sign goes with what variable:
% - Integer
& - Long
! - Single
# - Double
@ - Currency
$ - String
-
Aug 22nd, 2000, 01:28 PM
#15
Fanatic Member
What's wrong here in Belgium technical schools?
Everybody knows C/C++, but nobody knows what pointers are!
A large number of students already visited us at work for a job, and everybody was an expert in C...until we asked some stupid pointer/array questions.
I think pointers should also be implemented in Visual Basic.
I guess they are in C# (C sharp) but this would no longer be compatible with the VB versions...
-
Aug 23rd, 2000, 04:17 AM
#16
If you don't know pointers you don't know C or C++.
I'm glad that we don't have to use (C/C++ type) pointers in VB. I think that about 95% of all bugs in programs written in C/C++ is because of pointers or pointer assignment. And we actually don't need them in VB. We can pass arguments ByRef and we can use the AddressOf keyword for callbacks.
As far as I know C# will not have any pointers.
-
Aug 23rd, 2000, 12:36 PM
#17
Monday Morning Lunatic
Yes...although it must be more than 95% ...I've lost track of the number of times pointers have done my head in, especially when trying to find memory leaks.
Hint: use auto_ptr<Type T>...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 23rd, 2000, 01:02 PM
#18
if you cause a memory leak and you reboot, does it stop??
-
Aug 23rd, 2000, 01:05 PM
#19
Monday Morning Lunatic
Yes. I think it stops as soon as your program finishes executing. Memory leaks are only a problem while the program is running - the system frees all the allocated memory at the end. It's just that if your program will be running for a while, you'll eventually use all the free memory, then you'll really be screwed.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 23rd, 2000, 01:42 PM
#20
Fanatic Member
That's why I don't like to program in C++ for Windows 32 bits. I think it's a little complicated to obtain a silly running program like "Hello World".
VB uses more memory and require more runtime files but is a lot easier to create a working program.
But C gives us everything and VB not. That's why I mostly prefer to program embedded systems (Motorola, AVR, ...) in C languages like Hiware and IAR.
Also the (-inline-) assembler is VERY interesting. People should pay more attention to assembler and machine code. It' s the basic idea of logical programming!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|