I want to create and print 2-D char array in C, which i will eventually use as an input in function "execvp()" for execute and development of a shell. When i execute the code i take this as an output and not the whole array.
What is the problem?
In this part if the code i present only the part which i try to initialize the array:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
char **cmd[4][3] = {
{"ls", "-l",NULL},
{"grep", "test.c", NULL},
{"wc", NULL},
{"sort", "-r", NULL}
};
for(i=1;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%s\n",cmd[i][j]);
}
}
return (0);
}
Output:
grep
test.c
Segmentation fault (core dumped)
Hm. Why do you have for(i=1;i<5;i++) if you have only 4 rows?
Should it be
printf("%s\n",cmd[i-1][j]);
or
for(i=0;i<4;i++)
?
You have a 2 dimensional array of pointers to char. So the definition for cmd should be:
char *cmd[4][3] = {...
The for loop has incorrect indexes. for(i=1;i<5;i++) should loop from 0 to 4.
And you should check before trying to print NULL pointers, which definitely exist in the array:
if( cmd[i][j] )
printf("%s\n",cmd[i][j]);
The following program should give you an Idea:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void printArray(char **array,long unsigned int numWords){
printf("\n");
printf("Inside printArray:\n");
long unsigned int i;
for(i=0;i<numWords;i++){
printf("%s\n",array[i]);
}
printf("\n");
}
int main(void){
long unsigned int numWords,i;
char **array;
printf("Introduce how many words do you want:> ");
if((scanf("%lu", &numWords)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
array = malloc(sizeof(char*) * numWords * numWords);
if (array == NULL) {
exit(2);
}
for (i = 0; i < numWords; i++){
array[i] = malloc(sizeof(char*) * 100);
}
for (i = 0; i < numWords; i++){
printf("Enter your word number %lu:> ", i + 1);
if((scanf("%s", array[i])) != 1){
printf("Error, Fix it!\n");
exit(3);
}
}
printf("Inside Main:\n");
for (i = 0; i < numWords; i++){
printf("%s \n", array[i]);
}
printArray(array,numWords);
for (i = 0; i < numWords; i++){
free(array[i]);
}
free(array);
return 0;
}
Output:
Inside Main:
Michael
Jackson
Inside printArray:
Michael
Jackson
EDIT:
based on your question, the following program does what you need:
#include <stdio.h>
int main(void){
int i,j;
char *array[4][3] = {
{"A", "B", "C"},
{"D", "E", "F"},
{"G", "H", "I"},
{"J", "K", "L"},
};
for (i = 0; i < 4; i++){
for(j=0;j<3;j++){
printf("%s ", array[i][j]);
}
}
return 0;
}
Output:
A B C D E F G H I J K L
Related
Hello i am trying to find the frequncey of words in tow dimensional arry but im having problem with my codes so far this is what i have done i tried to find whats wrong using debugger but i cant find anything usefull im new to coding so i would be really happy if someone can tell me the problem
I pointed out where im having problem in my code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void freeFunction(char** arry, int size);
void insertArry(char** words, int countWords);
void wordFrequency(char** words, int countWords);
int main()
{
char** words=NULL;
int count = NULL;
insertArry(words, &count);
wordFrequency(words,count);
}
void insertArry(char** words, int* countWords)
{
char buffer[11];
int numberC;
printf("How many words?: ");
scanf("%d", &numberC);
getchar();
words = (char**)malloc(numberC * sizeof(char*));
if (words == NULL)
{
printf("No memory");
exit(1);
}
for (int i = 0; i < numberC; i++)
{
printf("Enter word: ");
gets_s(buffer, 11);
words[i] = (char*)malloc(strlen(buffer) + 1);
if (words == NULL)
{
freeFunction(words, i);
exit(1);
}
strcpy(words[i], buffer);
}
*countWords = numberC;
}
void freeFunction(char** arry, int size){
printf("No memory");
for (int j = 0; j < size; j++)
{
free(arry[j]);
}
free(arry);
}
void wordFrequency(char** words, int countWords)
{
int counter = 0;
for (int i = 0; i < countWords; i++) {
for (int j = i + 1; j < countWords; j++)
{
if(strcmp(words[i],words[j])==0) /// ***THIS IS WHERE MY PROBLEM RELAY ITS GIVING ME WEIRD ADDRESSES***
counter += 1;
}
printf("%s:%d",words[i], counter);
counter = 0;
}
}
I am trying to change the sorting of a the arr list which could consist of zero, one, two as the inputted and stored values for arr. The stringreplace function is meant to shift every single element by one so the new sorting would be one, two, zero. I am trying to replace the elements with one another by using the strncpy function but I think it is a bit faulty, how could i fix this?
strncpy function
char stringreplace( char a[], int b){
for(int j = 0; j > b -1; j++){
strncpy(a[j], a[j+1], sizeof(a));}
for(int j = 0; j > b; j++){
printf("%s",a[j]);}
}
main function
int main()
{
char input[100];
char arr[100]= {0};
int number;
printf("Input the number of strings: ");
scanf("%d", &number);
for(int i= 0; i < number; i++){
printf("Input the number of strings: ");
scanf("%s", input);
arr[i] = input;
}
stringreplace(arr, number);
return 0;
}
You may consider allocating strings dynamically, assigning a pointer for each string into an array words, and then rotating each pointer in the array to the left.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void lrot_words(char *words[], int n);
int main(void)
{
char *p, word[100], *words[100];
int i, num_words;
printf("Enter the number of words: ");
scanf("%d", &num_words);
for(i = 0; i < num_words; i++){
printf("Enter a word: ");
scanf("%s", word);
if ((p = malloc(strlen(word) + 1)) == NULL) {
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
words[i] = strcpy(p, word);
}
lrot_words(words, num_words);
for (i = 0; i < num_words; i++) {
printf("%s\n", words[i]);
}
return 0;
}
void lrot_words(char *words[], int n)
{
char *temp = words[0];
int i;
for (i = 0; i < n - 1; i++) {
words[i] = words[i+1];
}
words[i] = temp;
}
Just started learning programming on my own and whilst trying to create an array of characters from user input, using scanf, have hit the wall; the code is as below:
#include <stdio.h>
#define I 5
int main (void) {
char a[I+1] = {a[I+1] = '\0'}, q;
int i;
for(i = 0; i < I; i++) {
printf("Enter an alphabet:\t");
//scanf("%c", &q);
//scanf("%c*\n", &q);
//scanf("%[^\n]", &q);
//scanf("%[a-z, A-Z]", &q);
scanf("%127[^\n]", &q);
a[i] = q;
}
printf("\n");
for(i = 0; i < I; i++) {
printf("Element a[%d] of a[I]:\t%c\n", i, a[i]);
}
printf("And the a[I] string:\t%s\n", a);
return 0;
}
None of the scanf combinations in the above code works: the program either skips input prompt after the first one or does not store response.
How can this be resolved with scanf?
char a[I+1] = {a[I+1] = '\0'} is not valid. Even if it compiles, it is going out of bounds when assigning the '\0' character. The commonly used convention looks more like this instead:
char a[I+1] = {0};
Or simply:
char a[I+1] = {};
That said, q is only 1 char in size, but your scanf() is trying to read a string up to 127 chars into q. So you are going to trash memory. To read a single char at a time, use %c instead:
#include <stdio.h>
#include <string.h>
#define MAX_INPUT 5
int main (void) {
char a[MAX_INPUT+1];
int i, len;
for(i = 0; i < MAX_INPUT; i++) {
printf("Enter an alphabet:\t");
scanf("%c", &a[i]);
}
a[MAX_INPUT] = '\0';
printf("\n");
len = strlen(a);
for(i = 0; i < len; i++) {
printf("Element a[%d]:\t%c\n", i, a[i]);
}
printf("And the a string:\t%s\n", a);
return 0;
}
Or, you can remove the loop and just use a single call to scanf() using "%5[^\n]" as the format string:
#include <stdio.h>
#include <string.h>
#define MAX_INPUT 5
int main (void) {
char a[MAX_INPUT+1];
char fmt[20];
int i, len;
sprintf(fmt, "%%%d[^\n]", MAX_INPUT);
printf("Enter an alphabet:\t");
scanf(fmt, a);
printf("\n");
len = strlen(a);
for(i = 0; i < len; i++) {
printf("Element a[%d]:\t%c\n", i, a[i]);
}
printf("And the a string:\t%s\n", a);
return 0;
}
This works without any warning or error on Cygwin gcc v7.3 with Wall flag:
#include <stdio.h>
#define I 5
int main (void) {
char a[I+1] = {a[I+1] = '\0'},q;
int i;
for(i = 0; i < I; i++) {
printf("Enter an alphabet:\t");
scanf("%c%*c", &q);
a[i] = q;
}
printf("\n");
for(i = 0; i < I; i++) {
printf("Element a[%d] of a[I]:\t%c\n", i, a[i]);
}
printf("And the string a[I]:\t%s\n", a);
return 0;
}
I am passing a 2D char array to a function. If I print it in the calling function, I see it is filled with some elements. If I print it in the called function, only the first element is filled:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int compareTopString(char * string1, char * string2){
printf("%s %s\n", string1, string2);
return 5;
}
int getMaxTopics(int numP, int numTop, int * subject[][1000]){
for(int k = 0;k < numP;k++){
printf("%s\n", subject[k]);
}
return 0;
}
int main() {
int numP;
int numTop;
char subject[1000][1000];
scanf("%d", &numP);
scanf("%d", &numTop);
for (int i = 0; i < numP; i++){
scanf("%s", subject[i]);
}
printf("%d", getMaxTopics(numP, numTop, &subject));
for(int k = 0;k < numP;k++){
printf("%s\n", subject[k]);
}
return 0;
}
You are mixing char and int. Are you sure that compiled without warnings?
The programm falls in the infinite loop. But arr->count printf prints a normal value (4, for example). count has a type unsigned int and arr is a pointer to int. What's the problem here? loop prints arr values at first and then continues to print trash values
In arrat_get it prints array just fine
struct _arr {
size_t count;
int* arr;
} ;
typedef struct _arr array_t;
array_t* array_get(FILE* file){
int* arr = NULL;
size_t count = 0;
array_t* arr_t;
array_t temp;
int i = 0;
if (!file) {
fprintf(stderr, "there is no such file\n");
return;
}
if (fscanf(file, "%u", &count) == EOF) {
fprintf(stderr, "can't read count from file\n");
return;
}
temp = array_create(arr, count);
arr_t = &temp;
printf("%i\n", arr_t->count);
for (i = 0; i < arr_t->count; i++){
if (fscanf(file, "%d", &arr_t->arr[i]) == EOF) {
fprintf(stderr, "can't read arr from file\n");
return;
}
}
for (i = 0; i<arr_t->count; i++)
printf("%d ", arr_t->arr[i]);
printf("\n");
return arr_t;
}
int main(){
array_t* arr_t;
int i = 0;
printf("enter count and arr:\n");
arr_t = array_get(stdin);
printf("count in main=%u\n", arr_t->count);
for (i = 0; i<arr_t->count; i++)
printf("%d ", arr_t->arr[i]);
getch();
return 0;
}
This is not the solution of your problem, but may help you to find it:
#include <stdio.h>
#include <stdlib.h>
struct _arr {
int count;
int* arr;
} ;
int main(){
int i = 0;
struct _arr myArray[10];
for (i = 0; i < 10; i++) {
myArray[i].count = 9;
myArray[i].arr = &myArray[i].count;
}
for (i = 0; i < (myArray->count); i++)
printf("%d %p\n", myArray[i].count, myArray[i].arr);
return 0;
}
OUTPUT:
Compiling the source code....
$gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1
Executing the program....
$demo
10 0x7fffeb764c00
10 0x7fffeb764c10
10 0x7fffeb764c20
10 0x7fffeb764c30
10 0x7fffeb764c40
10 0x7fffeb764c50
10 0x7fffeb764c60
10 0x7fffeb764c70
10 0x7fffeb764c80