does anyone know why it throws following error when trying to run:
decrypt.c: In function 'main':
decrypt.c:14:30: error: invalid initializer
14 | char currentWord[] = argv[count];
|
It occurs in line 14. I want to pass every command line parameter into the char[] current word, but somehow it doesn't work.
#include <string.h>
#include <stdio.h>
int main (int argc, char* argv[]) {
for (int count = 1; count <= argc; count++) {
char tmp1;
char tmp2;
int countCurrent = 0;
char currentWord[] = argv[count];
int wordLength = strlen(currentWord);
int amountOfLetters = wordLength/2;
for (int i = 0; i < amountOfLetters; i++){
tmp1 = currentWord[countCurrent];
++countCurrent;
printf("Das ist der %i. Wert: %c\n",countCurrent, tmp1);
tmp2 = currentWord[countCurrent];
++countCurrent;
printf("Das ist der %i. Wert: %c\n",countCurrent, tmp2);
}
countCurrent = 0;
}
}
Thank you for any help :)
Best Enno
You can't initialize an array this way.
You need to:
char currentWord[strlen(argv[count]) + 1];
strcpy(currentWord, argv[count]);
Indexes are from zero in C language
for (int count = 0; count < argc; count++)
Related
Been trying to remove the first element from a double pointer char array but i keep getting errors.
The input into the argv is from the keyboard using:
argv is defined in main as
int main(int argc, char **argv)
if(!fgets(*argv, 64, stdin))
return 0;
for (int i = 0; i < argc; i++)
{
j = 0;
while(j < strlen(*argv) - 1)
{
if(j == 0)
strcpy(argv + j, argv + j + 1);
j++;
}
}
Error:
warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types]
22 | { if(j == 0) strcpy(argv + j, argv + j + 1);
| ~~~~~~~~~^~~
| |
| char **
Your code has many compile issues, first you need to define variable j and also using correct braces as follow:
#include <string.h>
#include <stdio.h>
int main(int argc, char **argv) {
if(!fgets(*argv, 64, stdin))
return 0;
for (int i = 0; i < argc; i++)
{
int j = 0;
while(j < strlen(*argv) - 1)
{
if(j == 0)
strcpy(argv + j, argv + j + 1);
j++;
}
}
}
After that, I am strongly against using argv for reading a string because you don't know anything about its size in memory. Also, when you are using the strcpy you must pay attention to the sizes, the destination string should have at least the size of the source.
strlen only works on strings which means char * so using it on char ** is meaningless. The following code copies each string into the left item which I think very similar to what you want to do but please note that string size is very important and strcpy can cause issue.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
const int LEN = 255;
int main(int argc, char **argv) {
int n = 3;
// allocate memory for storing strings
char **strings = malloc(n * sizeof(char *));
// reading strings from user
// and store them
for (int i = 0; i < n; i++) {
strings[i] = malloc(LEN * sizeof(char));
// reads in at most one less than size characters from stream and stores them into the buffer pointed to by s
fgets(strings[i], LEN, stdin);
}
for (int i = 0; i < n - 1; i++) {
// here we consider all the strings has the max size as LEN
// so this copy does not cause error.
strcpy(strings[i], strings[i + 1]);
}
free(strings[n - 1]);
for (int i = 0; i < n - 1; i++) {
printf("[%d] %s", i, strings[i]);
}
}
Also, you can use the non-dynamic way as follows:
#include <string.h>
#include <stdio.h>
#define LEN 255
#define N 3
int main(int argc, char **argv) {
char strings[N][LEN];
// reading strings from user
// and store them
for (int i = 0; i < N; i++) {
// reads in at most one less than size characters from stream and stores them into the buffer pointed to by s
fgets(strings[i], LEN, stdin);
}
for (int i = 0; i < N - 1; i++) {
// here we consider all the strings has the max size as LEN
// so this copy does not cause error.
strcpy(strings[i], strings[i + 1]);
}
for (int i = 0; i < N - 1; i++) {
printf("[%d] %s", i, strings[i]);
}
}
Both programs have the following behavior:
Parham
Ali
Hassan
[0] Ali
[1] Hassan
./a.out "1 23 5 7 2 21"
I want to convert the above string passed as a command line argument into an array of integers in C programming. Would really appreciate help.
Thank you.
A simple loop can solve your problem-
int a[argc];
for(i = 0; i < argc; i++)
{
a[i] = atoi(argv[i+1]);
}
If passing a string
./a.out "1 23 5 7 2 21"
You need to tokenize the string whilst passing the "int" value to an array (which should be dynamic) since you are passing a string and not multiple options. (Which was what I initially thought, but changed)
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* For strtok */
int main( int argc, char **argv )
{
int i = 0;
int *intArray;
const char s[2] = " ";
char *token;
token = strtok(argv[1], s);
intArray = malloc(sizeof(int));
while( token != NULL )
{
intArray[i++] = atoi(token);
token = strtok(NULL, s);
}
//intArray holds the values but this is to display the results
int j;
for (j=0; j < i ; j++){
printf( " %d\n", intArray[j] );
}
return 0;
}
If passing multiple options (after program name)
./a.out 1 23 5 7 2 21
int i;
int intArray[argc-1];
for (i=0; i < argc - 1; i++){
intArray[i] = atoi(argv[i+1]);
}
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]){
if(argc != 2){
exit(EXIT_FAILURE);
}
int n = 0;//number of elements
char prev = ' ', *s = argv[1];
while(*s){
if(isspace(prev) && !isspace(*s))
++n;
prev = *s++;
}
int nums[n];
char *endp;
s = argv[1];
for(int i = 0; i < n; ++i){
nums[i] = strtol(s, &endp, 10);
s = endp;
printf("%d\n", nums[i]);//check print
}
return 0;
}
I m writing a little C program and want to know why my output in the console is "0", "0" [...]? The output i expect is "ab", "ac", [...].
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
char output[8];
int length = size(&string[0]);
for(i=0; i<length; i++) {
for(j=0; j<length; j++){
char a = string[i];
strcat(output, &a);
char b = string[j];
strcat(output, &b);
printf("%c\n", output);
}
}
return 0;
}
Mistake #1. You have not initialised output[] so strcat() will not validly find a nul terminator to append to.
output[0] = 0;
Mistake #2. strcat() isn't the right way of appending chars anyway.
Mistake #3. Your loop controls aren't right. See below.
Mistake #4. Your length is the size of a char* pointer.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i, j;
char string[] = "abc";
char output[8];
int length = strlen (string); // corrected
for(i=0; i<length-1; i++) { // amended loop
for(j=i+1; j<length; j++) { // amended loop
output[0] = string [i];
output[1] = string [j];
output[2] = 0; // string terminator
printf("%s\n", output); // uses string type not char
}
}
return 0;
}
Program output:
ab
ac
bc
If I have understood correctly what you are trying to do then the program will look the following way
#include <stdio.h>
int main(int argc, char *argv[])
{
char string[] = "abc";
char output[3];
size_t length = sizeof( string ) - 1;
for ( size_t i = 0; i < length; i++ )
{
for ( size_t j = 0; j < length; j++ )
{
if ( i != j )
{
output[0] = string[i];
output[1] = string[j];
output[2] = '\0';
puts( output );
}
}
}
return 0;
}
The output is
ab
ac
ba
bc
ca
cb
If your compiler does not allow to declare variables within the control statement of the loop then you can declare i and j in the beginning of the program.
size_t i, j;
If you want to include combinations like "aa" then you simply may remove the if statement withing the inner loop.
char a = string[i];
strcat(output, &a);
leads to undefined behavior since strcat expects a null terminated string in the second argument. Same thing applies to:
char b = string[j];
strcat(output, &b);
Perhaps you meant to use:
output[0] = a;
output[1] = b;
output[2] = '\0';
Here's the updated for loop:
for(i=0; i<length; i++) {
for(j=0; j<length; j++){
output[0] = a;
output[1] = b;
output[2] = '\0';
printf("%s\n", output);
// ^^ use %s to print a string, not %c.
}
}
If you want to use strcat you must know that it expects a string not a character and there is an important difference, when you pass &a strcat thinks it is the address of a pointer to a string, and you should get most likely a segmentation fault, here I show your own code, modified to use strcat but you don't really need it for this task.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
int length = strlen(&string[0]);
for(i = 0 ; i < length ; i++)
{
for(j= i + 1 ; j < length ; j++)
{
/* initialize output to 0 more importantly to have a terminating null byte */
char output[3] = {0};
/*
* create a string and initialize it with 2 char's
* - the first one, the one you want to append to output
* - the second one is required by strcat, to mark the end of the string
*/
char a[2] = {string[i], 0};
strcat(output, a);
/* same as above */
char b[2] = {string[j], 0};
strcat(output, b);
printf("%s\n", output);
}
}
return 0;
}
you could do this without strcat unless you are trying to learn how to use strcat, this is an example of how to do it without strcat.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
int length = strlen(&string[0]);
for(i = 0 ; i < length ; i++)
{
for(j= i + 1 ; j < length ; j++)
{
char output[3] = {string[i], string[j], 0};
printf("%s\n", output);
}
}
return 0;
}
Since two hours, i'm trying to modify my program to give it arguments (argv) instead of a char.
So, here is my current code:
int i;
char ret[81];
*ret = 1;
for (i = 0; i < argc; i++)
{
ret[0] = '\0';
strcat(ret,argv[i]);
}
This code concatenate all args into a char, printf is returning the good same result as my old char argument, but not working in my code:
char test[] = "9...7....2...9..53.6..124..84...1.9.5.....8...31..4.....37..68..9..5.74147.......";
solve(test); //working
solve(ret); //not working
my app is launched like that:
./a.out "9...7...." "2...9..53" ".6..124.." "84...1.9." "5.....8.." ".31..4..." "..37..68." ".9..5.741" "47......."
Soooo, if anyone understand my problem i'll probably need some help :D
sample code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void solve(char *data){
static const char *result = "9...7....2...9..53.6..124..84...1.9.5.....8...31..4.....37..68..9..5.74147.......";
if(strcmp(result, data) == 0)
printf("working\n");
else
printf("not working\n");
}
int main(int argc, char *argv[]){
int i, total_length = 0;
for(i = 1; i < argc; ++i){
total_length += strlen(argv[i]);
}
char ret[total_length + 1];
ret[0] = '\0';
for(i = 1; i < argc; ++i){
strcat(ret, argv[i]);
}
char test[] = "9...7...."
"2...9..53"
".6..124.."
"84...1.9."
"5.....8.."
".31..4..."
"..37..68."
".9..5.741"
"47.......";
solve(test);
solve(ret);
return 0;
}
I know that getline is C++ standard but I need to read a line of digits:
123856
and save it to an array. But how to do this without spaces between given (as input) digits? I want a user input to be:
123856 (with no spaces) and then save it to an array (n element array) and after that, I want my array to look like this:
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 8;
array[4] = 5;
array[5] = 6;
But how to make it in C, without a getline?
This is NOT what I want:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char **argv)
{
int t[4];
int i;
for(i=0; i<4; i++)
scanf("%d", &t[i]);
for(i=0; i<4; i++)
printf("%d\n", t[i]);
return 0;
}
If I understood you correct, the following should do it:
read the whole line
loop through the string as long as you get digits or the string ends
for every digit, place it's value in your array and increase the index by 1
while( ( c = getchar()) != EOF && c != '\n' && i < max ) {
/* If desired, add check for value outside of 0-9 */
array[ i++ ] = c - '0';
...
}
char arr[] = "1234567";
int intarr[10];
int count = 0;
for (char* ptr = arr; *ptr; ptr++) {
intarr[count] = *ptr - '0';
count++;
}
try this
#include <stdio.h>
#include <string.h>
main (int argc, char *argv[])
{
FILE *f;
int i=0;
int j=0;
char output[100];
char* output1[100];
char string[100];
char delims1[] = " ";
char delims2[] = "*";
char* result = NULL;
char* result3 = NULL;
int num;
//for (j=0; j<2; j++)
//{
//printf("%s",delims9[6]);
//}
f = fopen("text.txt","r");
//
while( fgets(string,sizeof(string),f) )
{
result = strtok( string, delims1 );
while( result != NULL )
{
output1[i]=result;
printf("%s\n",output1[i]);
result = strtok( NULL, delims1 );
i++;
}
for (num = 0; num < 100; i++ ) //
{ // Error On this array
printf("%s\n", output1[i]); //
} //
}
printf("\n%d",i/3+1);
return 0 ;
}
Ok, without using any string.
int digits = 123856;
int numofdigits = 1 + floor(log10(digits));
int digits_arr[numofdigits];
int i;
for(i = numofdigits-1; i >= 0; i--) {
digits_arr[i] = (int)floor(digits / pow(10, i)) % 10;
}
Try the below link... Same question asked here and get solution....
convert an integer number into an array
char * convertNumberIntoArray(unsigned int number) {
unsigned int length = (int)(log10((float)number)) + 1;
char * arr = (char *) malloc(length * sizeof(char)), * curr = arr;
do {
*curr++ = number % 10;
number /= 10;
} while (number != 0);
return arr;
}