The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pass a pointer-to-pointer, or return a pointer? aaronwong High Level Programming 11 01-08-2009 01:03 PM
New Assignment vakharia Mahesh What's on Your Mind? 3 12-02-2007 04:07 PM
how to do my assignment????? amelia UNIX for Dummies Questions & Answers 1 01-19-2006 11:17 AM
@ in a variable assignment rkap UNIX for Dummies Questions & Answers 1 05-23-2005 10:53 AM
awk - value assignment. videsh77 Shell Programming and Scripting 2 12-31-2004 05:38 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2009
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
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 (permalink)  
Old 10-17-2009
julianjensen julianjensen is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 5
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 (permalink)  
Old 10-17-2009
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
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 (permalink)  
Old 4 Weeks Ago
HeavyJ HeavyJ is offline
Registered User
  
 

Join Date: Oct 2009
Location: Toronto
Posts: 18
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 "&"
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0