Gallery, Projects and General > Project Logs
X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
<< < (17/19) > >>
AdeV:
String comparisions (strcmp) are the bits that always get me in C; that, and pointers. Or dereferenced pointers.... I mean, what IS a dereferenced pointer, and why would one desire it?

FWIW, I've been programming in BASIC (in various incarnations - Sinclair SuperBASIC, BBC BASIC, GWBASIC, Visual Basic versions 3-6, VB.NET 1.0 & 1.1) for the last 24 years or so, so it's not like I can't program.... yet I've never ever had to (explicitly) work with a pointer...

kwackers:
I'm not sure why you have an issue with string comparisons although they do require pointers.

So quick pointer post.

A pointer is just an address to computer memory. If your machine has 64k of memory then it's a number from 0 to 64k.
Everything you type is in memory.

So in basic if you type:  let A$ = "hello world";
Then the string "hello world" sits in memory somewhere and A$ is probably internally a pointer to it.
(excuse my basic, it's 30 years since I last wrote any).

So say you look through memory and find your string at address 27000, then A$ would be the number 27000.

In C you do the same like this

char A[] = "hello world";

The variable A is a pointer to a type of 'char' (which is 8 bits per character - i.e. a string) by putting the []'s after it we're telling it that it's a pointer to an array of characters.
The string is placed into memory and the variable A is made equal to it's address.

Being a pointer to an array we can index using the brackets, so A[4] would be the letter 'o' (0 to length of string).

Normally you'll see pointers declared with asterisks like this:
char *B = A;
This declares a pointer to types of char named B and makes it equal to A

Therefore B[4] is also equal to 'o'...

Now because the pointer is not the string - it's the address of the string, if we look at it we'll just get the number (in our example we decided it was 27000).

But by using the []'s and looking at the 4th element in the examples above we're 'de-referencing' the pointer (and adding 4). De-referencing is simply the obtaining of the contents of the memory at the location pointed to by the pointer.

Without brackets we can dereference by using a '*' in front.

so *A is the same as A[0],  *(A+4) is A[4] etc etc (brackets used to make sure the addition happens first).

The main reason to dereference is to do pointer arithmetic and then look at what's in the address you calculated. Pointers can point to any type you have or can define (using structures etc).

If this makes sense then back to string compares.

char A[] = "my string";  // this string is 9 characters long - including the zero
char B[] = "my strang";

// pass the two pointers to our strings to strcmp
if (strcmp(A, B) == 0)
{
   // strings are equal
}

strcmp expects two pointers, in this case we created two pointers A & B that already point to some strings in memory.
the C language says strcmp returns zero if the strings are the same, so we use the 'is equals' operator '==' to check if its equal to zero.
Bluechip:

--- Quote from: craynerd on September 11, 2010, 08:07:19 AM ---
 ( 1 )  Download mikroC from the link I sent, it is the top download - MikroC compiler.

 ( 2 )  Take me for example, a dumb arse when it comes to PICs:



--- End quote ---

Hi Chris

( 1 )  Done ...

( 2 ) All very well, but what about we poor souls whose evolution has not reached that exalted stage ?   :scratch:

Thanks for the offer of help Kwackers ... will take you up on that  :thumbup:

I think this would be better in a separate thread ? Wonder how many others are interested in C PIC compilers ?? Gotta be some, world appears to be awash with nutters at the moment ...

So .. having pulled out a 16F877 LED flasher wotsit .. and ...

Q1  How do I change that hideous 'green text on a black background' screen. I can do it on MPASM, ( when I eventually found how to ).

Dave BC
raynerd:

--- Quote from: Bluechip on September 11, 2010, 11:12:40 AM ---
Q1  How do I change that hideous 'green text on a black background' screen. I can do it on MPASM, ( when I eventually found how to ).



--- End quote ---

Green text on a black background in MikroC ?? - mine starts up with the theme Whidbey in the Styles Toolbar
: View --> Toolbars --> Styles Toolbar     and select whidbey or another that isn`t green!     

Does that solve the issue? I`m wondering if we are talking about different things as I can`t see any view styles with black and green!
Bluechip:

Hi Chris

I give the Icon a wallop, things happen, and I land up with this ..




If I do the New Project screen, as per the getting started .pdf it does not work for me. I made a folder on C:\ and called a new project Prog_1

Clicked OK after fiiling in the bits it required. Refused to save it.

So, I got no new project screen. Dunno what that looks like. Must be better ... :scratch:

Dave




Navigation
Message Index
Next page
Previous page

Go to full version