Help understanding pointer assignment in c


 
Thread Tools Search this Thread
Top Forums Programming Help understanding pointer assignment in c
# 1  
Old 10-17-2009
Help understanding pointer assignment in c

after some years of pause, im returning to c.

char *varname = "asd";
int *number = 4;

the above code is wrong, because its assigning a value to an unreserved space, or because its changing the address the pointer is pointing ?

thanks for the replys!!
# 2  
Old 10-17-2009
The pointer number is undefined. You could do this, however:

int numberStorage;
int *number = &numberStorage;
*number = 4;

That example basically sets numberStorage to 4.

The string example is okay because the double quotes allocate static storage automatically.

Julian
# 3  
Old 10-17-2009
hmmm, so, with a "asdasd", the compiler takes care of alocating the memory

with the integer example, i have to use malloc, or create a normal integer, and point the pointer to the real integer

thanks for the clarification
# 4  
Old 10-19-2009
broli,

C does not hide memory pointer information.

"asdf" is a "string literal."

when initializing a char*, "asdf" is seen by the program as a pointer to that location in memory.

The same is not true for other basic types.

int *Bogus = { 0, 1, 2, 3, 4}; - does not work
int Correct[5] = { 0, 1, 2, 3, 4}; does work

malloc() allocates memory on a global heap, and requires a type cast.
using [] allocates memory locally, and the size of this chunk is limited, so don't use brackets for really big arrays.

Two operators will come in handy all the time... "*" and "&"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need a little help with assignment

Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions: Step 4: Modify your script so that if there is an argument called TestError you display the following error message with your usage statement. TestError found Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies

2. Homework & Coursework Questions

Understanding Getopts in an otherwise easy assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: In the command line I will be given files or words which will be USER ID's or a file of USER ID's. I am to let... (4 Replies)
Discussion started by: ByronicX
4 Replies

3. Programming

Understanding C++ template partial specialization with pointer datatype arguments

When I compile the below code, I am getting error as template<typename T> T AddFun(T i, T j) { return i + j; } template<> T* AddFun<T*>(T* i, T* j) { return new T(*i + *j); } int main() { int n = AddFun<int>(10, 20); int i = 10, j = 20; int* p = AddFun<int*>(&i,... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Homework & Coursework Questions

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory · By modification time when called with lm · By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a shell script which... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

5. Shell Programming and Scripting

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory · By modification time when called with lm · By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

6. Homework & Coursework Questions

Assignment Help

1. List commands to create the directory hierarchy $HOME/a/b/c in vi to replace all occurences of TMP with tmp in lines 1 through 10 in vi to replace first occurence of CPU_file with DISK_file at line 15 2. Explain with a very simple example, usage of "ls -a" 3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. What is on Your Mind?

New Assignment

All Sys Administrators, With due respect I would like to know what should be BEST Things to do when LEAVING one job , and what Precaution MUST be taken while taking over new JOB?? Please Discuss in detail the STEP to be taken for both the TIME ?? (3 Replies)
Discussion started by: vakharia Mahesh
3 Replies

9. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

10. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question