Sponsored Content
Full Discussion: help with data type sizes
Top Forums Programming help with data type sizes Post 302488131 by Driver on Saturday 15th of January 2011 09:41:46 AM
Old 01-15-2011
The reason pointer arithmetic is scaled for the base type is simply to support array operations.

Given

int x[2];

... we access the second element of the array by writing x[1] or equivalently *(x + 1). You cannot fully separate arrays and pointers because the value of an array is a pointer to its first element, such that most array operations will involve pointers to their elements. It would be quite inconvenient and error-prone if we had to multiply the element number with the base type size to access the element.

Quote:
for chars if i do a="a" then get the address (&a) i get some large negative number, but i know the size of a char is 1 byte or 8 bits, so shouldn't the value be between 0 and 255 ?

also if converting an address of an int to hex and printing it why is it never negative?
The address of your char has a different type - it's char *, not char - and will probably (but not necessarily) have the same size as any other pointer, and typically amounts to 32 or 64 bits on most systems.

Signedness is a matter of how the data is interpreted. The %x format specifier expects an unsigned argument and will therefore treat all bits as data, whereas if you're using %d, the most significant bit of your value will be treated as sign bit - if it's set, the number is negative, otherwise it isn't.

Pointer encoding is system-specific, but it is generally not signed, so you should not print it as a signed value.

Quote:
I think it's reasonably portable among 32-bit and 64-bit varieties of UNIX, and 32-bit varieties of Windows. Perhaps not 64-bit windows, in which long inexplicably doesn't change size even though the integer width of the processor doubles...
On Windows (and arguably other systems as well, though I tend to use unsigned long) it is best to convert your pointer to size_t, because that will give you an __int64.
 

10 More Discussions You Might Find Interesting

1. Programming

FILE data type

Hi all, Can anyone tell me a little about the datatype FILE, which represents stream. What does its structure look like, and in which header file is it defined and so on... Ex : FILE *fp ; fp = fopen("filename", "w") ; (6 Replies)
Discussion started by: milhan
6 Replies

2. Programming

time_t data type-- what does start +1 mean?

Hi, I am trying to understand an very old C program. .... time_t start, end; ptr = localtime(&start); ... fprintf(out, "%-35s 01 %5s %2s %10d 1 5 /tty/M%d/%02d %24s", buffer3, job, ver, start, mach_num,atoi(buffer), asctime(ptr)); fprintf(out, "%-35s 03 %5s %2s %10d 1 5... (9 Replies)
Discussion started by: whatisthis
9 Replies

3. Programming

data type limitation

I am writing some code to do analysis on the file system (HP-UX 11.11). I am using stat(..) to get file information. My problem is that the file-size may exceed the data types defined in 'sys/stat.h' & 'sys/types.h' respectively. Thus file-sizes in the Giga-byte range are not read correctly.... (2 Replies)
Discussion started by: ALTRUNVRSOFLN
2 Replies

4. AIX

Value too large to be stored in data type???

Hello, I get this message : "Value too large to be stored in data type" when I try to open a 3Gb file. Can someone helps me to resolve the problem. Thank you very much (5 Replies)
Discussion started by: limame
5 Replies

5. UNIX for Dummies Questions & Answers

Directorie listing in Human form for data sizes

I have seen it done at my job before, there is a command that will make a notepad and show the directorie path, subfolders, and size of the subfolders? But i dont want it to go lower than 2 levels for example: folder_01 10 GB subfolder_02 10 GB subfolder_03 10 GB... (4 Replies)
Discussion started by: JUSSAN007
4 Replies

6. Shell Programming and Scripting

Perl data type checking

I am using perl 5.8.0. I need to check some values to see it they are floats. Our system does not have Data::Types so I can't use is_float. Is there something else that I can use? The only thing in Data is Dump.pm. I am not allowed to download anything to our system so I have to use what I have.... (3 Replies)
Discussion started by: ajgwin
3 Replies

7. UNIX for Advanced & Expert Users

Memory allocation for float data type

Dear All, How internally memory allocated when we declare the float data type. how many bytes allocated for decimal and how many bytes for fraction. kindly help me in this regards. (2 Replies)
Discussion started by: rajamohan
2 Replies

8. Shell Programming and Scripting

Can I split a 10GB file into 1 GB sizes using my repeating data pattern

I'm not a unix guy so excuses my ignorance... I'm the database ETL guy. I'm trying to be proactive and devise a plan B for a ETL process where I expect a file 10X larger than what I process daily for a recast job. The ETL may handle it but I just don't know. This file may need to be split... (3 Replies)
Discussion started by: john091
3 Replies

9. Web Development

Data type to use for prices with commas

Hi everybody, I`m very new with PHP and Databases and I having the follow issue with prices data.. The original information is in CSV files. The prices have formatted with commas and dots as follow: 12,300.99 -->(thousands separated by commas) 3,500.25 -->(thousands separated... (10 Replies)
Discussion started by: cgkmal
10 Replies

10. Programming

Incompatible data type fpos_t in C

This is from a program I wrote over in 1998 that I am trying to compile on a linux machine: void write_line (FILE *fp, int rec_no, line_rec *arec) { fpos_t woffset; woffset = (rec_no - 1) * sizeof(line_rec); fsetpos(fp,&woffset); fwrite(arec,sizeof(line_rec),1,fp); }On the line... (2 Replies)
Discussion started by: wbport
2 Replies
BITARR(3pub)						       C Programmer's Manual						      BITARR(3pub)

NAME
ba_and_ba, ba_clear, ba_clear_all, ba_copy, ba_create, ba_destroy, ba_not, ba_or_ba, ba_or_not_ba, ba_query, ba_resize, ba_set, ba_xor_ba - bit array manipulation SYNOPSIS
#include <bitarr.h> Bitarr *ba_create(void); void ba_destroy(Bitarr *ba); Bitarr *ba_copy(const Bitarr *ba); int ba_resize(Bitarr *ba, size_t max_number); int ba_set(Bitarr *ba, unsigned number); int ba_clear(Bitarr *ba, unsigned number); void ba_clear_all(Bitarr *ba); int ba_query(Bitarr *ba, unsigned number); void ba_and_ba(Bitarr *ba1, const Bitarr *ba2); int ba_or_ba(Bitarr *ba1, const Bitarr *ba2); void ba_xor_ba(Bitarr *ba1, const Bitarr *ba2); void ba_or_not_ba(Bitarr *ba1, const Bitarr *ba2); void ba_not(Bitarr *ba); DESCRIPTION
These functions operate on bit arrays. ba_create creates one and ba_destroy it. ba_copy makes a copy of one, and ba_resize changes its size. The bit arrays resize themselves automatically, but ba_resize can be used to get rid of extra memory allocated for the array, or to make sure there is enough memory allocated for the array (so that further operations are guaranteed to work). ba_set sets one element (bit) in the array to 1. ba_clear clears one element (sets it to 0), ba_clear_all clears all elements. ba_query returns the current value of an element. ba_and_ba, ba_or_ba, ba_xor_ba, and ba_or_ba do logical operations on two arrays. The result will be stored in the first one. ba_not will invert all elements in the array. RETURNS
ba_create and ba_copy return a pointer to the new array, or NULL if they fail. Those that return an integer, return -1 for error, non-neg- ative for success. SEE ALSO
publib(3), iset(3) AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi) Publib C Programmer's Manual BITARR(3pub)
All times are GMT -4. The time now is 01:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy