How to edit the index of a pointer array? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok so I am curious as to how you would go about using a pointer array to access a value in the index. Like for example:
printf("%c", (*character)[0]);
I know I have this code wrong, but I have know clue how to fix it. Say I want to access the 0 position in the pointer array and then print it like above, how would I do it?

Assuming character is char character[] = {'1'};
character[someIndex] means someIndex[character] means *(character+someIndex)
If that is what you wanted to know. So you should be doing something like:
printf("%c", *(character+0));
Which is equivalent to
printf("%c", *character);
printf("%c", character[0]);
Just missed out - regarding this statement
index of a pointer array?
Please know Arrays are not Pointers. If that is where you were confused.

From your question I observe that you have declare a character pointer something like this
char *character="something";
and now you want access its contents by indexing.
So why worrying,No matter whether you have declare pointer array or array.You can access its contents by any of following way:-
printf("%c",character[1]); //general method
or by pointer notation
printf("%c",*(character+1));
or
printf("%c",*(1+character));//commutative law
or more surprisingly you can use following method too
printf("%c",1[character]);

char *arr[20]={"Stackoverflow"};
This means you have an char type pointer array where 20 memory location's addresses are stored in an 20 size array. 1st memory location in arr[0] points to the string "Stackoverflow" and rest of them are not assigned(so you will get garbage value or maybe segmentation fault). Now, if you want to access 0th memory location just do this:
printf("%s\n",arr[0]);

Related

Dereferencing a void pointer using its size in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that we cannot dereference a void pointer until and unless we typecast it.
void *ptr = call_a_function();
printf("%d",*(int *)ptr);
This informs the datatype of element(in this case its integer) to the compiler so that it can dereference the amount of space required for it(in this case its 4 bytes).
Suppose I dont know the final datatype, however I know the size of datatype. Can I still dereference it using only the size(4 bytes) and not the datatype(not using int)??
In other words is there a way to tell the compiler how many bytes of memory to read by providing the number of bytes to extract and not the datatype??
EDIT -
I needed to swap the contents of two void * pointers.
Got influenced by the regular way of swapping two values i.e storing the value of one pointer in a temporary variable and then proceed with swapping.
Was trying to attempt the same thing without knowing the datatype but only its size( since the function with this swap code accepts size of variable as one parameter ). However after reading the answers and comments, got to realize that I really dont need to dereference it. Probably memcpy is the correct way to go.
Suppose I dont know the final datatype, however I know the size of datatype. Can I still dereference it using only the size(4 bytes) and not the datatype(not using int)??
Since you cannot deduce or assume the data type, you can't dereference the pointer once to get the complete object. However, you can extract each byte and use it anyway you want.
void *ptr = call_a_function();
unsigned char* cptr = (unsigned char*)ptr;
for (int i = 0; i < num_bytes; ++i )
{
unsigned char c = cptr[i];
// Use c
}
In this case, the whole is not the sum of its parts. Casting not only provides the compiler with the size of the data but also how that data is to be interpreted. Suppose you get 4 bytes, this could be a string of three characters with a NULL byte or a signed integer, which could be encoded in big-endian or little-endian depending on the platform. So knowing the size is not sufficient to properly handle the data even if you could tell the compiler how many bytes to read/dereference.

char pointer value reassignment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
int main()
{
int i=0;
char *b,*a="a(a+b))";
b=a;
printf("%s",b);
printf("%d",a);
while(a[i]!='\0')
{
a++;
i++;
}
*a="(a+b)";
printf("%s",a);
}
initialy i am assigning a value to that char pointer
after moving it to the end using null comparision
can i again assign a value to that char pointer?
Can i do like this?
what is wrong here? can anyone please explain??
The reason your third line is failing is because youre trying to de-refrence and reassign a value to a "string" value on the stack. ( which is very very bad, please never do this if it ever lets you )
If you do want it to work, the compiler has to create a new string value and reassign the pointer so you would have to change the code to this.
a = "(a+b)";
This will create a new value on the stack and reassign the pointer to the beginning of that string.
However if you are going to use strings like that. PLEASE use const char *. Its ultimately safer and saves you a lot of headaches.

Easy way to deal with free in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I have a function that gets a pointer int *vector with a couple int values. I want to delete element number n. So I will use free() on that element. The problem I have now that there is a "hole" in my array of int values. Is there an easy way that I dont have this problem or do I really have to make a new int pinter and reorder my vector?
Given a function of this form:
void delete_element(int *vector, size_t index) {
// ...
}
The actual argument corresponding to vector is expected to be a pointer to a series of one or more (implied: index + 1 or more) contiguous ints. This could be part or all of an ordinary int array, or it could be a dynamically allocated block. If the former, then you cannot free any part of the space at all. If the latter, then you can free or reallocate the whole space, but not just the part associated with one element.
To avoid the deletion leaving a hole in your array, you need to move the later elements down, and to do that, you need to know how many elements there are in total. Therefore, you need a more informative function signature, perhaps something like this:
void delete_element(int *vector, size_t *size, size_t index) {
// ...
}
The actual deletion might involve simply using memmove() to move the later elements (overwriting the one to be deleted), and then decrementing the size. With respect to the latter, note that I suggest passing a pointer to the vector size, so that the function can modify the caller's copy.
If you want also to shrink the allocation then you need to do a bit more work (involving calling realloc(), and communicating the revised value of vector back to the caller), but note that in that case your function will not work on ordinary arrays.
There is no way to free() part of a block returned by malloc(). If you want to delete record[n], you need to copy record[n+1]...record[last] into the array.
If you really need to free() each element, you must first malloc() each element.

iterating pointer gives array subscript is above array bounds error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have seen allot of posts about the array subscript is above array bounds error and i understand it happends when the iterator equals the max size of the array but my case is different:
static inline void setUninitialized(uint32 * xoBuff, uint32 xiItemSize)
{
uint32 i;
for (i=0 ; i < (xiItemSize/sizeof(uint32)) ; i++)
{
*(xoBuff++) = (uint32)MAP_UNINITIALIZED_VALUE;
}
}
when i run this code i get the array subscript is above array bounds error but i cant figure out why, i have literally tried all the posible combinations of incrementing the iterator and casting all kinds of variables.
the definition of uint32 is:
typedef unsigned long uint32;
any ideas why this keeps happening?
EDIT:
the way that the function is called is as follows:
TableEntry sEntry;
setUninitialized((uint32 *)&sEntry.policyKey, sizeof(PolicyKey));
the policyKey field is of instance PolicyKey
The line
sizeof(PolicyKey)
doesn't give you the size of the array. You can't ever get the size of an array in C. Rather, it's giving you the size of the pointer PolicyKey (assuming that's what it is), which will always be the same size (probably 8). So then (xiItemSize/sizeof(uint32)) will always (again assuming that everything on your computer is normally-sized) evaluate to 2.
It's hard to say anything else without knowing what sEntry looks like, but my guess is that you pass in a buffer that's only one-item long at some point, and it segfaults when it tries to dereference *(xoBuff++).

how to initialize and use a 2d struct in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So what i am trying to do is have an array of lists, here is my code:
typedef struct stackList{
List * list;
} stack;
int main(){
int x;
stack ** stackTable;
for(x=0;x<100;x++)
stackTable[x]=malloc(sizeof(stack*)*100);
}
i get a segmentation fault on the for loop, i would assume the way i am trying to use the struct is wrong. Would i rather in the defintion of the struct use List ** list;
or is there a way to use it the way i am trying to use it
You get segmentation fault because you're accessing stackTable while it is uninitialized. You can't know to what address of memory it points, and you haven't allocated an array to hold the pointers that you are dereferencing.
You need to make stackTable point to a valid array of pointers, in this case I think is convenient to make it be an array:
Stack* stackTable[100];
Now you have an array of pointers to Stack, you can initialize them.
If instead you have just temporarily an array large 100, and you need to make it grow in future, that's how dynamically allocating it:
Stack** stackTable= malloc(100*sizeof(Stack*));
Before trying too hard to play with pointers and dynamic memory I might suggest writing some basic programs using basic 2d arrays. For instance:
char array2d[10][10];
Once you're confortable inserting elements into this array, extracting elements, etc, you can apply all of the same principles to any type.

Resources