What is the difference between datatype* and regular datatype?

This is a discussion on What is the difference between datatype* and regular datatype? within the C Programming forums, part of the General Programming Boards category; So, I am currently learning the C language in class and I am still somewhat confused about pointers. My question ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    12

    What is the difference between datatype* and regular datatype?

    So, I am currently learning the C language in class and I am still somewhat confused about pointers.

    My question are the following:

    What is the difference between initiating char with with a star:
    Code:
    char* num;
    than with regular char
    Code:
    char num;
    ?



    What does it mean when a function of char data type is initiated with a star next to it, e.j.:
    Code:
    char* function(......) {}



    Thanks!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    Try reading this Pointers in C - Tutorial - Cprogramming.com

    A pointer only holds an address NOT the value of what it points to.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    • Welcome to the forum
    • Code:
       char num
      means that we declare a variable of type char with name num.In other words,we allocate memory for the variable to live in and we name it as num.Notice that we have not given a value to this variable yet.
    • [code]char* num [code] means that we declare a char pointer with name num.In other words,we allocate memory for a pointer to line in,thus we allocate memory for an integer,because the pointers are ready to hold addresses.Addresses of what?Addresses of what they are going to point to.So we have this memory allocated and now the pointer is ready to point to a char,or at sequence of char's ( a string ).Notice that not only we haven't set the pointer to point somewhere (so it is a good practice to write
      Code:
      char* num=NULL;
      (NULL is declared in stdlib.h header .NULL means that the pointer points to nothing)),but we have not allocated memory for what the pointer is going to be pointing in the rest of the program.
    • The style of most functions is
      Code:
       returnValue function(..){..}
      .So the char* is the type of the return value..At first make sure you get this
      Code:
      int foo(void);
      
      int main(void)
      {
            int x;
            x=foo();
            return 0;
      }
      
      int foo(void)
      { 
            return 5;
      }
      In this example x is going to be set to 5.

      Exactly the same way you should imagine that the
      Code:
       char* foo(..){..}
      works.This is very useful when we want to return an array of chars,because an array is equivalent to a pointer to it's first cell

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum!

    A pointer is just the house or apartment number where you live -- except it is the address of a variable (maybe), or NULL, (for safe parking only), or just a random "garbage" address, because it was never given an address.

    Don't try and overthink pointers. They just hold an address, instead of some other data type, in C. Using the * with them, (after they are initialized), refers to the value of what they are "pointing" to, in their address.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    12

    Talking

    Thank-you for all the answers, I really appreciated your time and effort! I got some more questions as a result of your guys' description. So I often see a pointer made (we'll go with our original examples) with a following line

    Code:
    num = (char*) malloc(sizeof(char));
    So, with an existing pointer, you can essentially expand it's address size, am I correct?


    Furthermore, if I want to take individual char data type from an array, one by one using the for loop, what do I need to do to combine them to form a string?


    For example, if I have the following array:

    Code:
    char int_array[5] = {1, 2, 3, 4, 5};
    and the char pointer:
    Code:
    char* num = NULL;
    What do I need to form the string "12345" as a whole thing, pointed by num?

    Thanks!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    (char *) malloc(etc.), is called "casting the return from malloc", and in C (as opposed to C++), you should not be doing that (dataType *) casting.

    The pointer can't be changed in size, it is fixed. The size of the space it points to, can be changed - when you need a bigger space, you can either remalloc() or simply malloc a larger space, and free() the old one.

    New with C (and lots of compilers don't support it yet), are data structures that will expand themselves (arrays). My compiler doesn't support this yet, so I have not worked with it.

    The easy way to make a string out of a bunch of digits in an array of char, is to use the function called atoi(). It takes a string of digits, and turns it into an integer for you:

    So
    Code:
    int myIntVariable = atoi(int_array);
    atoi() is part of stdlib.h, which you should add to your include file list at the top of your program.

    If you want to use a char * (char pointer) to point to the char array you call int_array[], (THAT NAME!), then the easy way would be to make your int_array, one char bigger, and add the end of string char, onto the end of the digits.

    This is just some chars: 01234586abcd
    This however, is a string: 01234586abcd\0.

    The end of string char: '\0', is not seen when you print the string, but it is ALL important when changing "some chars", into a string for the C program.

    Anyway, back to the topic.
    Code:
    char int_array[6] = {"12345"};
    Will automatically have an end of string char added to it, so you can print it as a string with
    Code:
    printf("%s\n",int_array);
    If you wish. The name of the array serves as a constant pointer to the base of the array.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    12
    Wow, thank you Adak! I'm so glad my friend advised to visit this forum -- so far, it has been a much better experience than talking to my professor/TA during SUITABLE office hours. Hopefully I can start doing some work now...I shall be back whenever new questions arises, hehe.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're quite welcome, and Yep! it's a dandy forum for C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. datatype bit?
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 02-05-2009, 01:56 PM
  2. Datatype
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 08-04-2008, 01:53 PM
  3. C datatype, enumerator
    By onebrother in forum C Programming
    Replies: 3
    Last Post: 07-26-2007, 01:49 AM
  4. about byte datatype
    By harish13 in forum C Programming
    Replies: 6
    Last Post: 07-20-2006, 02:25 PM
  5. DataType* MyVar;
    By GetAGrip in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21