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?
Related
this is my first question on StackOverflow ! :)
To be honest I'm about to destroy my whole setup.
My code is making me crazy.
My problem is, I am not able to fill a dynamic array with the return of a function.
My goal here is, for each array box, fill it with a random value of 'randomizer'. I am not able to take the return of randomizer in the array box.
Here is the code:
main.c:
#include "functions.h"
#include "functions.c"
/* TP 3 - ESIEE-IT Rémy JARDIN */
int main() {
int saisie, i;
printf("Creation du Tableau. \nNombre de caractere du tableau : ");
scanf("%d", &saisie);
ArrayCreate(saisie);
// Affichage
return 0;
}
functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
int ArrayCreate(int saisie);
int randomizer();
int insereAIndice();
#endif
functions.c:
#include <stdio.h>
#include <stdlib.h>
int ArrayCreate(int saisie) {
int i;
int *Tab = (int *)malloc(saisie * sizeof(int));
if (Tab == NULL) {
printf("Not enough Memory");
exit (1);
}
for (i = 0; i < saisie; i++) {
Tab[i] = (randomizer + 1);
}
printf("\n Resultats : ");
for (i = 0; i < saisie; i++) {
printf("%d - ", *(Tab + i));
}
return 0;
}
int randomizer() {
//int x = rand() % (100 + 1);
return 1;
}
And the error is:
functions.c: In function 'ArrayCreate':
functions.c:12:8: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
Tab[i] = (randomizer+1);
Instead of Tab[i] = (randomizer + 1); you should write:
Tab[i] = randomizer();
Note also these remarks:
the function prototypes in functions.h should have an argument of void:
int randomizer(void);
int insereAIndice(void);
file functions.c should include functions.h to ensure consistency between function declarations and definitions.
Writing *(Tab + i) is much less readable than Tab[i]. If you wish to obfuscate the code, use i[Tab] which is equivalent :)
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int ArrayCreate(int saisie) {
int i;
int *Tab = (int *)malloc(saisie * sizeof(int));
if (Tab == NULL) {
fprintf(stderr, "Not enough Memory\n");
exit(1);
}
for (i = 0; i < saisie; i++) {
Tab[i] = randomizer();
}
printf("\n Resultats : ");
for (i = 0; i < saisie; i++) {
printf(" %d", Tab[i]);
}
printf("\n");
return 0;
}
int randomizer(void) {
// return a random integer in the range 1..100
return 1 + rand() % 100;
}
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 tried making a small program that would detect if you typed in a palindrome but for some reason, it just loops
ps I'm a beginner
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[100], arr1[100];
int i;
printf("type in a string\n\n");
gets(arr);
strrev(arr) == arr1;
for (i=0; arr==arr1; i++)
{
printf("%c is a palindrome\n", arr);
}
for (i=0; arr!=arr1; i++)
{
printf("%c is not a palindrome\n", arr);
}
return 0;
}
arr and arr1 are base address of the two arrays respectively which would be different.One simple Code is here
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[100], arr1[100];
int i;
printf("type in a string\n\n");
gets(arr);
int len=strlen(arr);
strcpy(arr1,arr);
strrev(arr);
for(i=0;i<len;i++){
if(arr1[i]!=arr[i]){
printf("Not palindrome");
return 1;
}
}
printf("Palindrome");
return 0;
}
Use fgets instead of gets.
The first character could be compared to the last character. Then move the indexes toward the center for subsequent comparisons.
#include <stdio.h>
#include <string.h>
int main(void) {
char arr[100] = "";
int first = 0;
int last = 0;
printf ( "type in a string\n\n");
if ( !fgets ( arr, sizeof arr, stdin)) {
printf ( "fgets problem\n");
return 0;
}
arr[strcspn ( arr, "\n")] = '\0';//remove newline
for ( first = 0, last = strlen ( arr) - 1; first <= last; first++, last--) {
if ( arr[first] != arr[last]) {
printf("%s is not a palindrome\n", arr);
return 0;
}
}
printf ( "%s is a palindrome\n", arr);
return 0;
}
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
I am having a few issues with my code. First: when I try to compile, I get error: too few arguments to function 'strcmp'. I have looked all over and made multiple changes and am still unable to get it to work. Second: when my code does compile (if I remove the strcmp part), it will not complete the count functions correctly. Can anyone please assist? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}
This is not right way to use strcmp.
n = strcmp(char string1[], char string2[], char string3[]);
strcmp is used for compararison of string. See doc
int result = strcmp (string1,string2)
If strings are same, function will return 0.