1

I want to know whether a pointer is pointing to an array or single integer. I have a function which takes two pointer (int and char) as input and tell whether a pointer is pointing to an array or single integer.

pointer=pointer+4;
pointer1=pointer1+4;

Is this a good idea?

4
  • 1
    There is no portable way to do this. Commented Apr 5, 2016 at 16:35
  • @Donnie, not that much of an unportable way either. Commented Apr 5, 2016 at 16:35
  • 2
    OP: Why do you need to know whether it is pointing to an array or single integer? What problem are you really trying to solve? Commented Apr 5, 2016 at 16:38
  • @SergeyA you can occasionally get allocation sizes and try to guess based on the size of the primitive, but yeah, you're still just guessing. Commented Apr 5, 2016 at 16:39

4 Answers 4

2

Like others have said here, C doesn't know what a pointer is pointing to. However if you should choose to go down this path, you could put a sentinel value in the integer or first position in the array to indicate what it is...

#define ARRAY_SENTINEL -1

int x = 0;
int x_array[3] = {ARRAY_SENTINEL, 7, 11};

pointer = &x_array[0];

if (*pointer == ARRAY_SENTINEL)
{
   // do some crazy stuff
}

pointer = &x;

if (*pointer != ARRAY_SENTINEL)
{
   // do some more crazy stuff
}
Sign up to request clarification or add additional context in comments.

5 Comments

That works only if you can guarantee that a single int object that the pointer points to doesn't happen to contain the value -1. (Incidentally, the macro should be parenthesized, and you misspelled SENTINEL.)
Also you might have missed that we are talking C++ not C.
0xDEADBEEF (or similar) would make a much better sentinel than -1.
This is true in both C and C++. Neither are able to know what a pointer is pointing to. And yes you would need to come up with a unique sentinel value. I don't recommend doing what I did above, but wanted to answer his question. Macro is fine.
Also I neglected to cast in the "if" comparison but since author is using this to determine int versus an array of int, it's not necessary.
2

That's not a good idea. Using just raw pointers there's no way to know if they point to an array or a single value.

A pointer that is being used as an array and a pointer to a single values are identical - they're both just a memory address - so theres no information to use to distinguish between them. If you post what you want to ultimately do there might be a solution that doesn't rely on comparing pointers to arrays and single values.

6 Comments

A pointer is not "used as an array". Arrays and pointers are distinct things. See section 6 of the comp.lang.c FAQ.
@KeithThompson, I disagree with your comment. Pointers are not arrays, but pointers can be used as an array. There is nothing wrong with this answer.
@SergeyA: What exactly does it mean to "use a pointer as an array"?
@KeithThompson, use an array subscript operator, for example. (don't tell me that array subscript operator can be used in integral constant, it's not the point). Pointers can also participate in pointer arithmetics, which is only defined for arrays.
@KeithThompson, still, in laymen terms, this is considered the mother of all array uses (and the operator name even has word array in it).
|
0

Actually pointers point to a piece of memory, not integers or arrays. It is not possible to distinguish if an integer is single variable or the integer is an element of array, both will look exactly the same in memory. Can you use some C++ data structures, std::vector for example?

Comments

-1

For C++ questions, the answer is simple. Do not use C-style dynamic arrays in C++. Whenever you need a C-style dynamic array, you should use std::vector.

This way you would never guess what the pointer points to, because only std::vector will be holding an array.

5 Comments

I agree with that anonymous downvoter. There is applications, where dynamically allocated array is more (slightly) effective and thus better to use. As far as I know 'std::vector' is used for dynamic work - where you want to add and remove elements in unpredictable way, but If you need to write some algorithm, where size of an array is known and won't change during execution, 'std::vector' is totaly overkill. For example [link] (en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm).
But I guess, that the downvote is for your offtopic answer.
@RainbowTom, you are wrong. C++ vector provides same or better performance as compared to dynamic C-style array. And I do not see it as on offtopic, it is very offtopic, because it suggests the better way of doing the same thing.
Can you give me some better arguments? Link to some docs, blog, etc. I am a student, I'd like to learn that :)
@RainbowTom, start with google. 'performance of std::vector vs C-style array' is a good starting query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.