Hello,
The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41.
And my code is:
Thanks a lot!
Last edited by yifangt; 01-17-2014 at 05:50 PM..
Reason: adding tags
Here String "Hello" is stored in Character Array 'a', where size is 7. here array 'a' stores characters in contiguous Memory Location. It will take following form after initialization.Each array location will get following values - and last 2 are unused.
In this method element is accessed Sequentially
char *a = "HELLO";
String "Hello" will be stored at any Anonymous location in the form of array. We even don't know the location where we have stored string, However String will have its starting address.
here Address = [Base Address of Anonymous Array] + [i]
suppose you have to access a[3] then you need to get pointer value, add 3 to the pointer value, and gets the character pointed to by that value.
This User Gave Thanks to Akshay Hegde For This Post:
Thanks Akshay!
So the problem is, at Line 40 &41 of my code, the pointers (*text, *a) that were only declared but not allocated to memory, while the arrays text[100], and a[100] are allocated to memory automatically when they were declared. Is my understanding right? I do not have clear ideas on these points.
And, when I tried your suggestion:
There is a bug that seems to be related to the memory allocation.
It seems to me *a only gets 7 byte plus '\0' at the end, total 8 bytes. Why not 64 bytes as allocated at Line 41?
The declarations:
allocate space on the stack for two pointers to characters. And the size of a is the size of a pointer (4 bytes in a 32-bit application; 8 bytes in a 64-bit application).
With these declarations, the initial value of these pointers is whatever random bytes happen to have been on the stack. When you copy data or read data into an area pointed to by an uninitialized pointer, you will get a memory fault, a bus error, or overwrite data at some random location depending on what random bytes on the stack happen to underly your pointers. If you malloc() space for arrays of characters and assign the pointers that malloc() returned to your pointers, then you won't be overwriting a random location in memory, but you would still have a problem because the sizeof(a) in:
is the size of the pointer; not the number of bytes allocated by malloc() to the array pointed to by a.
The declarations:
allocate two arrays of 100 bytes each on your stack. And the size of a is 100 bytes.
Thanks Don!
Got your points about the array and pointer. But, when the substring is less than 8 char long, the output is correct, which seems the first 7 bytes are "not randomly overwritten". Why?
So it seems I have to use array in the original function (Line 40, Line 41). Then, what is the correct way if pointer is used? Thanks a lot again!
because the sizeof(a) in:
is the size of the pointer; not the number of bytes allocated by malloc() to the array pointed to by a.
And, since you're getting 8 bytes for sizeof(a), we know that you are building your application as a 64-bit app; not as a 32-bit app. You don't want sizeof(a) when a is a pointer; you need to use the number of bytes allocated to the space pointed to by that pointer instead.
This User Gave Thanks to Don Cragun For This Post:
I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here... (1 Reply)
Hi guys,
Besides the points bellow, what would best practices for scripting be ?
1) set the PATH
2) unset the current environment (set -u ?)
3) (re)set the IFS to default value - space (IFS="" <- is this correct ?)
4) check the return code for each action inside the script (cd, rsync,... (1 Reply)
if i create an array of pointers to a structure "struct node" as:
struct node *r;
and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as:
r=multiplty(.......) /*some parameters*/
is... (2 Replies)
Hi all,
Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this...
Say I declare a typedef to an array of pointers to some type...
/**
* An array of ptrs to sections
*/
typedef... (4 Replies)
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)
All ..
I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced
code:
int cnt1;
char *t_array;
char *f_array;
for(cnt1=0; cnt1<1000; cnt1++)
{
t_array =... (1 Reply)
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)