9

I'm new to C.

I know this has been asked in many forms but mine is a little unique...I guess. I have an unsigned short pointer.

6 unsigned short *pt;  
7 pt = myArray[0];

The array is declared as such: const unsigned short myArray[1024] and is an array of hex numbers of the form 0x0000 and so on.

I try to compile, it throws these errors:

myLib.c:7: error: data definition has no type or storage class
myLib.c:7: error: type defaults to 'int' in declaration of 'pt'
myLib.c:7: error: conflicting types for 'pt'
myLib.c:6: note: previous declaration of 'pt' was here
myLib.c:7: error: initialization makes integer from pointer without a cast

any ideas of what's going wrong?

Thanks, Phil

2
  • 2
    Note that representations such as hexadecimal, decimal and octal only come into play for integer literals in source code. There is no such thing as hex numbers (or decimal numbers or what-have-you) when it comes to the compiled program. Integers use whatever format the platform provides (usually 2's complement). As such, myArray is simply an array of const unsigned shorts, not "hex numbers". Commented Jul 10, 2011 at 10:58
  • @outis: Excellent observation about an important programming concept. I've added it to my software concept inventory. Commented May 14, 2015 at 15:55

4 Answers 4

12

My guess (you only show two lines) is that this code appears outside a function. This is a statement:

pt = myArray[0];

Statements must go in functions. Also, if myArray has type unsigned short[], then you want to do one of these instead:

pt = myArray;
pt = &myArray[0]; // same thing
Sign up to request clarification or add additional context in comments.

2 Comments

Ah...wow yes it was outside of a function! Thank you! I moved it to the correct place and most of my errors have gone away. So now I have: 22 unsigned short *pt; 23 pt = &myArray[0]; in the main() of my function. Yes for some reason I still get Program.c:23: error: assignment discards qualifiers from pointer target type Any ideas on this? Do i need to show more lines?
If your array has type const unsigned short[1024], then your pointer should have type const unsigned short *. The qualifier being discarded is const. Try using the clang compiler instead of gcc, the error messages are more informative.
9

& is the reference operator. It returns the memory address of the variable it precedes. Pointers store memory addresses. If you want to "store something in a pointer" you dereference it with the * operator. When you do that the computer will look into the memory address your pointer contains, which is suitable for storing your value.

char *pc; // pointer to a type char, in this context * means pointer declaration
char letter = 'a'; // a variable and its value

pc = &letter; // get address of letter
// you MUST be sure your pointer "pc" is valid

*pc = 'B'; // change the value at address contained in "pc"

printf("%c\n", letter); // surprise, "letter" is no longer 'a' but 'B'

When you use myArray[0] you don't get an address but a value, that's why people used &myArray[0].

1 Comment

strictly speaking pointers got pointer value which includes memory address. As it could have other things as well in it apart from just address
4

Yeah, you really should include a bit more code so we can see the context.

I don't quite get the error messages, but your code is not correct.

Try:

pt = &myArray[0];

Or:

pt = myArray + 0;

Or just:

pt = myArray;

Instead.

4 Comments

I'm a bit curious what the motivation is for pt = myArray + 0;?
@GavinH: The motivation is pedagogical.
@John: Sure, it demonstrates pointer arithmetic but there are compelling reasons to use option 1 or option 3 but I don't think such reasons exist for option 2. Adding pt = &0[myArray]; would also work but wouldn't provide any real motivation for use.
@GavinH I don't see the answer as providing three choices. I see the second one as "showing one's work" to get from the first to the third. I didn't suggest that there was additional practical value to the second by itself.
0

You should not throw away the const qualifier; so the pointer should have a const modifier.

const unsigned short myArray[1024];
const unsigned short * pointer = myArray;  // set pointer to first element

3 Comments

I think (but I might be mistaken) that your answer would apply to C++ but not to C (which the OP is using).
Nope, I just tested it with MinGW's C++ Compiler (g++). It compiled without any warnings and the pointer worked for reading the array and (when I removed the const modifier) for writing to the array.
Oh OK. Well I just tried it in gcc (C) also and it works. I've used this kind of assignment many times in C, in many different compilers. I misread your comment and assumed you were skeptical about the C++ case.

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.