Is it a pointer or a variable?

Debojit Acharjee 435 Reputation points
2023-05-27T05:28:11.7533333+00:00

I want to know what is the difference between using the asterisk (*) before a pointer and after the "char" keyword? If "char a" means 'a' is pointer then while initializing it with a text why the asterisk () is not used before it? Does the compiler take it as a character variable or an integer variable containing t he memory address of the text?

Please see the following program:

#include <stdio.h>

char *food = "ICE CREAM";
char* drink = "COCA COLA";

int main ()
{

food = "BURGER";

    printf("%s, %s\n", food, drink);
    printf("%d %d", food, drink);

    return 0;
}

The output of this program shows the second initialized text "BURGER" and globally initialized text "COCOA COLA", when the pointer is converted to integer, it shows two integer values. What are these values?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,873 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,634 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,630 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 61,416 Reputation points
    2023-05-28T16:15:15.8+00:00

    In C a pointer is an unsigned int. The sizeof(int) is the size of a pointer. Nowdays typically 32 or 64 bits. When you print a pointer as an int, you are printing the memory address it points to.

    Also in C, the string literals datatype is char*.

    Note: when printing a pointer you should use %i or large address may print as negative. Actually it’s common practice to print addresses as hex (%x).

    Also C datatype declarations are one of its most difficult syntax’s to understand. The usage decorators are on the variable rather than the type (common on more modern languages)

    // pointer to int array

    int *myvar1[];


2 additional answers

Sort by: Most helpful
  1. Barry Schwarz 2,511 Reputation points
    2023-05-27T07:03:10.0066667+00:00

    char a DOES NOT mean a is a pointer.

    char *a means a is a pointer. The white space between the type (char), the asterisk, and

    the variable name (a)is irrelevant. char*a, char* a, char *a, and char * a all mean the

    exact same thing.

    Since both food and drink are pointers, it is undefined behavior to print their values with %d.

    You should be using %p. However, on most systems the undefined behavior produces the expected

    results. In this case, the value printed for food is the address of the B in the string literal

    "BURGER". Similarly, the value printed for drink is the address of the first C in "COCA COLA".

    Finally, BURGER is not initialization text. Initialization can occur only when the variable is

    being defined. food was originally initialized to point to "ICE CREAM". BURGER shows up in an

    assignment statement that results in a new value being ASSIGNED to food.

    1 person found this answer helpful.

  2. Lamia Elshahat Eldesoky 76 Reputation points
    2023-05-29T04:54:55.6566667+00:00

    The same answer