-5

My problems occurred as I was attempting to write linked lists in c. I got the program to work by following various tutorials, but I don't fully understand the use of pointers. When I get into designing my own structs/lists they don't work out and I am getting many floating point errors in compilation. I looked it up and this happens when pointers are misused. I don't want someone to fix my code - because, then I wouldn't learn what I am trying to learn. If someone could, please, explain the difference between:

char c;
char *c;
char c[];

and how and/or when to use them then.

Paul R
  • 208,748
  • 37
  • 389
  • 560
James
  • 517
  • 1
  • 4
  • 17
  • 3
    What book are you using to learn C ? It sounds like you might need [a better one](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Paul R Nov 24 '15 at 07:56
  • 1
    This isn't a suitable question for SO so it will get closed. However, search the site: see [this](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) and [this](http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c). – Lundin Nov 24 '15 at 07:58
  • `When I get into designing my own structs/lists they don't work out and I am getting many floating point errors in compilation`. What's a '*floating point error in compilation*'? Next time try and narrow that down to a small, self contained code snippet that exhibits the error, which others can compile/debug and look at. It would vastly increase your chances of getting any meaningful help. – dxiv Nov 24 '15 at 08:31
  • Perhaps I asked my question incorrectly. If I wanted to create an unspecified number of struct(s) of unknown size or 'structure', what form of data structure should I start with and how should I link it so that I can access members dynamically, ie: by selection rather than cycling through the whole thing? My end result would be a dynamic array of pointers to structures (I include full databases as a single array of structs). – James Nov 24 '15 at 08:39

2 Answers2

2

char c is a treasure chest. char *c is a paper where one could put down a treasure map. It can be a fake map, or it could even be blank. char c[] is an album of treasure maps, none of them fake or blank (and one of them being on the cover, so you can use the album itself as the first treasure map in it).

Err, okay. The first c contains a number from -128 to 127. The second one is a pointer to character; it tells you where a character is. Or it just says NULL, so you know it is not pointing at anything. The third one is a location where a character is; unlike a pointer, you can't change it, it always points to an already allocated location, and it has a size; but you can use it in a lot of cases as if it was a pointer.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Are you suggesting that `char c[]` is a list of pointers? A better analogy is that `char c[]` is an atlas. Then again, posts like these are C's [burrito tutorials](http://blog.plover.com/prog/burritos.html). They often have original analogies, but [are misleading](https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/) to beginners who are just trying to understand the fundamentals of the language. –  Nov 24 '15 at 08:53
0

Ok, char c is the definition of a variable which contains a 8 bit number. The char* c is a variable which contains the address of a variable which stores a 8 bit number. So the first thing you have to understand is the meaning of the * and & operators: The & operator means "get the address from". So for example if you have something like:

char c = 'a';

Then you have declared and defined a variable called "c". The content of this variable is the letter "a".

Now lets declare a pointer:

char* cPtr;

As you already know is "cPtr" a variable which contains the address of a char variable. So let's assign the address of our variable "c" to our pointer variable "cPtr":

cPtr = &c;

Now our pointer variable contains the address of the variable "c". What we can do now is to access the content of the variable "c" via our pointer "cPtr" by prepending our pointer variable with the * operator. So the * operator means something like "get the content of the variable to which this pointer is pointing to". When I write something like:

char b = *cPtr;

Then I create a new variable "b" and assign the value of the variable "c". It is equivalent to char b = c because I have dereferenced the pointer variable cPtr, which means I used the pointer (address) stored in "cPtr" to access the content of c.

Now we create an array of char values:

char cArray[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

This array is a sequence of 6 char values which are placed in the same order in the memory. So in the first place in the memory you have the value 'H', the next place in the memory contains the value 'e' and so on. The difficult thing here is that you alwys need the index to access the single values. So if you write something like:

char a = cArray[1];

Then you copy the 'e' of the array to the variable "a".

Now the confusing thing is that an array can also be treaded like a pointer:

char* arrayPtr;
arrayPtr = cArray;

This means that you assigned the address of the first array element to the pointer variable arrayPtr. So you have to know if you use the array name without the index then you'll use the pointer to the first element of the array.

So if you create a string using

char* myString = "Hello";

then (under the hood) you create an array of char variables like shown with cArray.

gmug
  • 773
  • 5
  • 11
  • Thanks for that. That does answer a couple questions I forgot to ask. I find that the questions people see as "stupid", are the ones that often lead to the right questions that I should be asking. By char* some_thing = something_else, I say that some_thing creates temporary memory allocation for 'some_thing' based on the properties of 'something_else '? – James Nov 24 '15 at 08:56
  • @James No, by saying `char* some_thing;` you are creating a pointer called "some_thing" which only contains an address. Assuming you have declared `char something_else;` then your statement is incorrect. You can only assign the address of "something_else" using the `&` operator: `char* something = &something_else`. – gmug Nov 24 '15 at 09:03
  • @James Nobody thinks your question is stupid, Its just not suitable here. Go through [help center](http://stackoverflow.com/help) – r2_d2 Nov 24 '15 at 09:07
  • Thanks. Apparently I still have a lot to learn about pointers. Might be a good idea to learn more about them before I get too carried away with data structures and design patterns. – James Nov 24 '15 at 09:19