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 came across this snippet:
#include<stdio.h>
int main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
return 0;
}
Output: Garbage Value ---- 1
This was the explanation:
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access
the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array.
However, I am still unable to understand the same. I would like the same to be provided in an easy to understand manner (Not that I am complaining about the above explanation).
Explanation on the 6th and 7th line may please be provided.
There are several problems with your code:
#include<stdio.h>
int main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
a is declared as a 3D array, but you initialize it with a 2D array.
int *p,*q;
p=&a[2][2][2];
p is initialized to an invalid memory location. Since a has only 2 elements per dimension, the only valid subscripts are 0 and 1.
*q=***a;
q has not been initialized to point to a valid location in memory. Derferencing q with *q is undefined behavior.
printf("%d----%d",*p,*q);
return 0;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to c language after learning python and java and I encountered an interesting exercise that i cannot find answer online for.
Given an *char array of pointers to string (*char arr[N(N is defined)]={"sadsd","gasgs"......})
what is the proper way to pass given array of string pointers to a function so that the function will be able to modify the array?
For example if i want to print the array with a different position for each element in it? After i pass the array pointer to the function I cant modify the array for some reason without using [] do define the elements I want to work on. the exercise is to use pointer arithmetics without using [].
thank you and have a great day :)
Passing an array of strings to a function can be done with a declaration like this :
void function(char ** strArray);
The size cannot be found from such a pointer so it should be passed too :
void function(char ** strArray, int arraySize);
Indices are accessible with [] from 0 to arraySize-1. Pointer arithmetic allow doing the same with a different syntax :
strArray[5] // Gives a string (whose the size could then be found with strlen)
*(strArray + 5) // Equivalent form using pointer arithmetic
Here is an example printing the array content after having changed the second index :
void printStringArrayContent(char ** array, int size) {
*(array + 1) = "Margaret";
for(int i=0; i<size; i++) {
printf("%s ", *(array + i));
}
printf("\n");
}
void main(){
char * array[3] = { "John", "Sarah", "Michael" };
printStringArrayContent(array, 3); //==> John Margaret Michael
}
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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
This is just a part of the code and it is simplified for this question.
I would like to be able to store the word into my struct, the whole word not just the first char.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
int main(void) {
char a[]="";
a[0]='w';
a[1]='h';
a[2]='i';
a[3]='l';
a[4]='e';
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
}
this only prints w and I need it to print while
I cannot use anything other than the char a[]="";
a must be build in this way
When I print result it is while but when I put inside the struct it is only w
By doing struct lexics rs = {WHILE_KEYWORD, *a};, you are effectively putting the first element of a inside of the lexics.lexeme array. Try using a function that copies the entire array:
struct lexics rs = {WHILE_KEYWORD};
strncpy(rs.lexeme, a, LEXEME_MAX - 1);
rs.lexeme[LEXEME_MAX - 1] = 0; // to be sure that the string is properly terminated :)
In your code
char result[]="";
does not give you an infinite-length array, it's a one element array (only null-terminator). You need to either have a proper size mentioned, or a long-enough initializer to have a proper size of the array.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
...
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
In C arrays are filled with zero if there are less initializers than elements in the array.
In your code you only provide 1 single char to initialize char lexeme[LEXEME_MAX] array. This means the array looks like "w\0\0\0\0..." and as a result only the first character is printed.
You must use strcpy or strncpy to copy the string from array a into the array rs.lexeme.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to create a function which elaborates an array. I'm not sure how to input a vector or an array on a function and then returning the modified vector/array (I'm trying to use pointers).
Is this possible or I'm using pointers in the wrong way?
int newValue(int *p){
// modify the vector
return p;
}
int main(){
int a[6]={4,6,7,3,1,8};
int *p;
p = a;
p = newValue (p);
(This is the assignement:
Given an array of VET_SIZE elements of integers:
Write a function to insert a new value in a particular index of
the array, and move the following elements forward without
deleting existing values except for the value of last element)
I'm not sure how to input a vector or an array on a function and then reurning the modified vector (I'm trying to use pointers)
All you need to do is to pass the array itself, you can then modify it directly in function newValue. Something like this:
void newValue(int *p, size_t n){
// modify the 2nd item
p[1] = 10;
}
Then in main just call it:
newValue(a);
A couple of notes:
Your newValue does not need to return anything (void is ok), as you can change the array directly.
void newValue(int *p, size_t n) is the same as void newValue(int p[], size_t n) - because array decays to pointer.
Usually it's good to pass an additional parameter n about the input array size, so inside your function you can check to make sure you don't have out-of-bound access.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please help, I need to read an input txt file into an array and print it out put somehow I keep getting error message.
#include <stdio.h>
void reading_into_array(int A[]);
#define MAXVALS 100
int
main(int argc, char *argv[]){
int numbers[100], i;
reading_into_array(numbers[MAXVALS]);
for(i = 0; i < 100; i++){
printf("%d", numbers[i]);
}
return 0;
}
/*input information*/
void
reading_into_array(int A[]){
double inp;
int n = 0;
while(scanf("%lf",&inp) == 1){
A[n++] = inp;
}
}
numbers[MAXVALS] is out-of-range and its type doesn't match with the function argument. use numbers instead.
Avoid using values of uninitialized variables having automatic storage duration, which invokes undefined behavior. Initialize numbers like int numbers[100]={0},i;
When calling a function that takes an array as a parameter, you only need to supply the name of the array, e.g. numbers. "numbers[MAXVALS]" would supply the value of the MAXVALth element of this array. This is wrong for two reasons:
the function needs an array, not an element
The array has a size MAXVAL; its elements are counted from zero to MAXVAL-1, so the MAXVALth element does not even exist
If you want floating point numbers in your array, declare the array as double A[MAXVAL] everywhere.
Last note: the reading_into_array function should have a check that it will prevent it from putting more than MAXVAL numbers into the array, or you risk that it will corrupt memory and crash your program.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code: array of structures that must be sorted. Program works, but:
I can't understand, if Mat is pointer, why not void sort(tArt *sMat), but void sort(tArt sMat[]). I'm really puzzled.
typedef struct{
char data[26];
}tArt;
...
int main(void)
{
FILE* fMat; fMat=fopen..........
tArt* Mat;
...
Mat=malloc(sizeof(tArt));
for(i=0;i<N;i++) fread(&Mat[i],sizeof(tArt),1,fMat);
fclose(fMat);
sort(Mat,N);
...
}
void sort(tArt sMat, int num) {...........}
My guess since we can not see the entire code, is that when you use malloc to dynamically allocate the array you forget to allocate the array for N 'objects'. In other words, I suspect your problem lies in the line
Mat = malloc(sizeof(tArt));
where it should be
Mat = malloc(sizeof(tArt) * N);
On the other hand, when you create explicitly your array with a declaration of the form
tArt Mat[N];
where N is defined somewhere earlier in the ellipses, everything is working as expected.
Hope this helps.
There is no difference between tArt *sMat and tArt sMat[] and tArt sMat[1234], the compiler treat them all as tArt *sMat and ignore the length information.