Thread: int* vs int[]

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    Question int* vs int[]

    Today I took a test at my university about C. The question was:
    What is the difference between the pointer to the int value and the int array, i.e., int *p vs. int p[]?

    My answer was that there is a diffence. You can change the value of the pointer (int*) and sizeof(array) return the whole size of the array not just size of the pointer. But when I use it as function arguments then they are both viewed as int*.

    My teacher is saying that it is wrong and they are the same.

    Who is right? Please take into consideration the whole question because maybe I interpreted it in a wrong way.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,337
    You are right, and given what you wrote, you should be able to easily construct a program to demonstrate it to your teacher just as you described.

    Of course, if the question had been something like: "What is the difference between the syntax int *p vs. int p[] when used to declare a function parameter?" then indeed your teacher would be correct, but as it explicitly specified "the pointer to the int value and the int array", your teacher is unequivocally wrong according to the C standard.
    Last edited by laserlight; 11-11-2020 at 06:29 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    2
    Thank you very much for your help. You helped a lot

Popular pages Recent additions subscribe to a feed

Tags for this Thread