c - char array only prints first char [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 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.

Related

char* array of pointers modification [closed]

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
}

Inserting new values on vector (in C language) [closed]

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.

C - Array of structures [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 4 years ago.
Improve this question
im having a txt file
abc=123 def=456 ...
I have defined my struct like this:
typedef struct rule {
char* old;
char* new;
}Rule;
I have counted the ammount of these rules via function
int count_rules();
Now I'm calling this function in another, while making dictionary of rules
void make_dic(){
ammount_rules = count_rules();
//here goes the problem
Rule *dictionary = malloc(ammount_rules * sizeof(Rule));
}
I want to scan another txt and replace old with new so I'd like to acces every twin with simple command
for (i=0; i<ammount_rules;i++){
if ( (string_in_text) == (dictionary.old[i]) )
{
printf("%s" dictionary.new[i]);
}
}
Your malloc seems to be fine. It allocates an array of Rule with ammount_rules elements.
However, the way you use dictionary seems wrong.
dictionary.old[i] // wrong
dictionary[i].old // correct
dictionary.new[i] // wrong
dictionary[i].new // correct
BTW: Notice that you are comparing pointers - not strings. Use strcmp for comparing strings.

Creating a 2D array of strings 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 5 years ago.
Improve this question
Could also be called a 3D array, as each string is essentially it's own array.
Would be in the following format, each row = a new 'order'.
[ [FirstName, SecondName, DOB, Newspaper] ]
[ [FirstName, SecondName, DOB, Newspaper] ]
The contents of the array will be all strings (even though DOB would be in the format "23012017" and then converted to integer if necessary
Tried to use this "pointer arrays", but am not sure about how to use it.
char *bookings[][2];
char firstname[20], secondname[20], dob[8];
char *bookings[][0]=firstname, *bookings[][1]=secondname, *bookings[][2]=dob;
I think what you need is a struct and a normal 1D array. Like:
struct order {
char FirstName[42];
char SecondName[42];
char DOB[42];
char Newspaper[42];
}
and in your code (e.g. in main)
struct order[42];
Then you do:
strcpy(order[0].FirstName, "Donald");
strcpy(order[0].SecondName, "Duck");
... and so on

Explanation required - 3-D Arrays, Pointers [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 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;
}

Resources