asdf
asdf

Reputation: 667

Understanding pointers in C in terms of strings

I have just started a new class at my university and it kind of has jumped straight into C without learning all the syntax and semantics (which isn't too bad to pick up on). However the one big difference compared to languages I do understand (java, python) are the concept of pointers.

I know that:

& - Address of something
* - value stored at the address of something

So if I have a string like:

char a[] = "ABCDEF";

Does 'a' have an address associated with it (&a) and if I do (*a) does it reference the entire string? The first character in the 'array' (A)?

Going off that thought seeing how it is a char [] does every single character in the string have its own address?

My eventual goal is to write a function that can manipulate pointers in order to locate the first character in one string that matches the character in a second string.

Am I on the right track? Note this is all pseudocode since I still am trying to learn C syntax

#include <stdio.h>

int main() {
    create address to 'a'
    create address to 'b'
    make 'a' a string like "abcdefg"
    make 'b' a string like 'b'
    call findMatch(a,b);    //pass in both to a function
    return 0;  // I know I have to have this at the end
}

void findMatch(char a, char b){
    Retrieve the pointer to the first character in the 'a' string
    Increment through 'a' to see if it matches the dereferenced b
    If 'a' contains 'b' print its in the string as well as the address of the location in 'a'
}

example run - findMatch("abcdef","f") gives a print statement that says 'f' is in 'abcdef' and the address location of 'f'

I have read there are build in libraries with string functions in C but I want to manipulate the pointers myself to learn.

Thanks

Upvotes: 0

Views: 133

Answers (3)

Will
Will

Reputation: 2121

In C:

  • A pointer is an address. You can pretty much use these terms interchangeably.
  • An array variable is a pointer to (i.e., address of) the first element of the array it references.
  • A string is just an array of chars (or wchars for wide non-ascii characters).

When your code contains a declaration like:

char a[] = "ABCDEF";

The compiler allocates sufficient memory for the entire character array plus a trailing \0 terminator byte in an appropriate place (e.g., the stack), and writes the bytes accordingly.

Remember your variable a is now the address of the first letter, 'A'; and as such you can use it like a pointer. For example, accessing its value with the * operator (called dereferencing) and comparing it:

*a == 'A' // commonly written as: a[0] == 'A'

will evaluate to true. Accordingly all of the following evaluate to true as well:

*(a + 1) == 'B' // commonly written as: a[1] == 'B'
*(a + 2) == 'C' // commonly written as: a[2] == 'C'
*(a + 3) == 'D' // commonly written as: a[3] == 'D'
*(a + 4) == 'E' // commonly written as: a[4] == 'E'
*(a + 5) == 'F' // commonly written as: a[5] == 'F'
*(a + 6) == '\0' // commonly written as: a[6] == '\0'

Let me know if any additional clarification is needed.

Upvotes: 3

rsethc
rsethc

Reputation: 2664

To put this as simply as possible:

char* str1 = "Hello world, goodbye world."; char* str2 = *str1;

You will assign to the pointer value str2 the char value at the location pointed to by str1. Since 'H' is 0x48 in ASCII, the location pointed to by str2 becomes 0x48. So if you dereference str2, you will be attempting to read one byte at location 0x48 in memory.

It's extremely unlikely, but possible, that str1 is actually located starting at location 0x48 in memory, and would work that one time. But by no means should you rely on THAT.

What you may mean to do here is char* str2 = str1;. In this case, str2 now refers to the same location that str1 did UPON ASSIGNMENT (so if you change str1 but leave str2 unaltered, you may have issues when dereferencing str2 if you have deleted the memory is pointed to). But they are still referring to the same block in memory, and so changing something about str1 or str2 will throw off str2 or str1 respectively, either changing the contents of the block that BOTH point to, or making one invalid because the memory has been moved somewhere else. It depends on the operation being done.

If you want a separate copy, you must replicate the memory block. size_t len = strlen(str1); str2 = new char [len]; memcpy(str2,str1,len); Then, you would have to delete them separately, too. delete [] str1; would not affect str2, and delete [] str2; would not affect str1. They are not connected in any way if the copy is cloned and in a separate block of memory.

Moral of the story is that you are not really dealing with strings as entities, but just in concept. You are dealing with arrays of characters just like you would deal with arrays of integers or arrays of pointers (which technically are arrays of integers, just of a specific kind).

Upvotes: 0

Hot Licks
Hot Licks

Reputation: 47729

Consider that in C all of RAM is one gigantic array of char. There is really nothing (substantial) dividing one conceptual entity from another.

You could have a string in RAM as the bytes H o w . n o w . b r o w n . c o w (zero byte) and immediately after that the bytes 0x01 0x02 0x03 0x04 representing the balance in my retirement account. If you update the string to read "How now chartreuse cow", you will write "cow" over my retirement balance and I'll be in the poorhouse (if I was not there already). (Though actually "cow" would probably be an improvement.)

RAM is one gigantic char array, and a pointer of any type is just a pointer into that array. Nothing "marks" a location in RAM as being one type or another, and, given that you can do arbitrary arithmetic on pointers, you can pretty much access anything (within your process address space) or modify anything there, whether it's meaningful to do so or not.

Upvotes: 0

Related Questions