I have the following code, the code crashes whenever the strcpy function is called. When these lines are commented out the code does not crash. What is wrong?
char cities[80][17];
char char_distances[40][2];
int distances[40];
char cities_sorted[20][17];
while (!feof(Text)) {
fscanf(Text, "%[^\t]\t%[^\t]\t%[^\n]\n",
cities[i], cities[i + 1], char_distances[i]);
distances[i] = atoi(char_distances[i]);
printf("City_start: %s City_end: %s Distance: %d \n",
cities[i], cities[i + 1], distances[i]);
static char uniqueCities[21][17];
int uniqueCitiesCount;
for (int j = 0; j < 21; j++) {
printf("%s\n", uniqueCities[i]);
bool start_unique = !areEqual(cities[i], cities[j]);
bool end_unique = !areEqual(cities[i], cities[j + 1]);
if (start_unique) {
strcpy(uniqueCities[uniqueCitiesCount], cities[i]);
uniqueCitiesCount++;
}
if (end_unique) {
strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]);
strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]);
uniqueCitiesCount++;
}
}
i++;
}
Thanks
What's wrong?
Well many things:
you posted a code fragment: this is not enough information to get help diagnosing your problem. The code posted cannot be compiled and tested, does not even have definitions for all the symbols in the fragment: how is Text defined? how was it opened? is it guaranteed to be different from NULL?
i is not defined in the fragment, how is it defined? is it initialized?
while (!feof(Text)) is not a good way to test for end of input, you should instead compare the result of fscanf() with the expected number of conversions.
fscanf(Text, "%[^\t]\t%[^\t]\t%[^\n]\n", does not have enough information to avoid buffer overflows. The maximum number of characters to store into the arrays should be specified this way: fscanf(Text, "%16[^\t]\t%16[^\t]\t%1[^\n]\n",. Note however that if the input is not consistent with these limitations, conversions will fail and the rest of the input file will be read out of sync. You shoud read the lines into a line buffer and use sscanf() to parse the lines.
char char_distances[40][2]; defines an array of array that can only contain 1 character and a null terminator. The distances must all be expressed as a single digit in the input file. You should probably define the arrays with a larger size or convert the distance directly into the distances array with a %d conversion specifier.
int uniqueCitiesCount; defines a local variable that is not initialized. Using it in your loop invokes undefined behavior as you probably try to access beyond the end of the 2D array.
printf("%s\n", uniqueCities[i]); will print an empty string as no city has yet been copied to this array.
bool start_unique = !areEqual(cities[i], cities[j]); how are bool and areEqual() defined?
strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]); is duplicated. The index i + 1 is incorrect.
The logic in the loop is contorted and probably flawed. You should just compare the city name with all entries in the uniqueCities and only add it the this array after the loop if it has not been found.
Related
I'm pretty new to C, and I'm trying to write a function that takes a user input RAM size in B, kB, mB, or gB, and determines the address length. My test program is as follows:
int bitLength(char input[6]) {
char nums[4];
char letters[2];
for(int i = 0; i < (strlen(input)-1); i++){
if(isdigit(input[i])){
memmove(&nums[i], &input[i], 1);
} else {
//memmove(&letters[i], &input[i], 1);
}
}
int numsInt = atoi(nums);
int numExponent = log10(numsInt)/log10(2);
printf("%s\n", nums);
printf("%s\n", letters);
printf("%d", numExponent);
return numExponent;
}
This works correctly as it is, but only because I have that one line commented out. When I try to alter the 'letters' character array with that line, it changes the 'nums' character array to '5m2'
My string input is '512mB'
I need the letters to be able to tell if the user input is in B, kB, mB, or gB.
I am confused as to why the commented out line alters the 'nums' array.
Thank you.
In your input 512mB, "mB" is not digit and is supposed to handled in commented code. When handling those characters, i is 3 and 4. But because length of letters is only 2, when you execute memmove(&letters[i], &input[i], 1);, letters[i] access out of bounds of array so it does undefined behaviour - in this case, writing to memory of nums array.
To fix it, you have to keep unique index for letters. Or better, for both nums and letters since i is index of input.
There are several problems in your code. #MarkSolus have already pointed out that you access letters out-of-bounds because you are using i as index and i can be more than 1 when you do the memmove.
In this answer I'll address some of the other poroblems.
string size and termination
Strings in C needs a zero-termination. Therefore arrays must be 1 larger than the string you expect to store in the array. So
char nums[4]; // Can only hold a 3 char string
char letters[2]; // Can only hold a 1 char string
Most likely you want to increase both arrays by 1.
Further, your code never adds the zero-termination. So your strings are invalid.
You need code like:
nums[some_index] = '\0'; // Add zero-termination
Alternatively you can start by initializing the whole array to zero. Like:
char nums[5] = {0};
char letters[3] = {0};
Missing bounds checks
Your loop is a for-loop using strlen as stop-condition. Now what would happen if I gave the input "123456789BBBBBBBB" ? Well, the loop would go on and i would increment to values ..., 5, 6, 7, ... Then you would index the arrays with a value bigger than the array size, i.e. out-of-bounds access (which is real bad).
You need to make sure you never access the array out-of-bounds.
No format check
Now what if I gave an input without any digits, e.g. "HelloWorld" ? In this case nothin would be written to nums so it will be uninitialized when used in atoi(nums). Again - real bad.
Further, there should be a check to make sure that the non-digit input is one of B, kB, mB, or gB.
Performance
This is not that important but... using memmove for copy of a single character is slow. Just assign directly.
memmove(&nums[i], &input[i], 1); ---> nums[i] = input[i];
How to fix
There are many, many different ways to fix the code. Below is a simple solution. It's not the best way but it's done like this to keep the code simple:
#define DIGIT_LEN 4
#define FORMAT_LEN 2
int bitLength(char *input)
{
char nums[DIGIT_LEN + 1] = {0}; // Max allowed number is 9999
char letters[FORMAT_LEN + 1] = {0}; // Allow at max two non-digit chars
if (input == NULL) exit(1); // error - illegal input
if (!isdigit(input[0])) exit(1); // error - input must start with a digit
// parse digits (at max 4 digits)
int i = 0;
while(i < DIGITS && isdigit(input[i]))
{
nums[i] = input[i];
++i;
}
// parse memory format, i.e. rest of strin must be of of B, kB, mB, gB
if ((strcmp(&input[i], "B") != 0) &&
(strcmp(&input[i], "kB") != 0) &&
(strcmp(&input[i], "mB") != 0) &&
(strcmp(&input[i], "gB") != 0))
{
// error - illegal input
exit(1);
}
strcpy(letters, &input[i]);
// Now nums and letter are ready for further processing
...
...
}
}
I'm trying to create a complete C program to read ten alphabets and display them on the screen. I shall also have to find the number of a certain element and print it on the screen.
#include <stdio.h>
#include <conio.h>
void listAlpha( char ch)
{
printf(" %c", ch);
}
int readAlpha(){
char arr[10];
int count = 1, iterator = 0;
for(int iterator=0; iterator<10; iterator++){
printf("\nAlphabet %d:", count);
scanf(" %c", &arr[iterator]);
count++;
}
printf("-----------------------------------------");
printf("List of alphabets: ");
for (int x=0; x<10; x++)
{
/* I’m passing each element one by one using subscript*/
listAlpha(arr[x]);
}
printf("%c",arr);
return 0;
}
int findTotal(){
}
int main(){
readAlpha();
}
The code should be added in the findTotal() element. The output is expected as below.
Output:
List of alphabets : C C C A B C B A C C //I've worked out this part.
Total alphabet A: 2
Total alphabet B: 2
Total alphabet C: 6
Alphabet with highest hit is C
I use an array to count the number of the existence of each character,
I did this code but the display of number of each character is repeated in the loop
int main()
{
char arr[100];
printf("Give a text :");
gets(arr);
int k=strlen(arr);
for(int iterator=0; iterator<k; iterator++)
{
printf("[%c]",arr[iterator]);
}
int T[k];
for(int i=0;i<k;i++)
{
T[i]=arr[i];
}
int cpt1=0;
char d;
for(int i=0;i<k;i++)
{int cpt=0;
for(int j=0;j<k;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>cpt1)
{
cpt1=cpt;
d=T[i];
}
printf("\nTotal alphabet %c : %d \n",T[i],cpt);
}
printf("\nAlphabet with highest hit is : %c\n",d,cpt1);
}
There is no way to get the number of elements You write in an array.
Array in C is just a space in the memory.
C does not know what elements are actual data.
But there are common ways to solve this problem in C:
as mentioned above, create an array with one extra element and, fill the element after the last actual element with zero ('\0'). Zero means the end of the actual data. It is right if you do not wish to use '\0' among characters to be processed. It is similar to null-terminated strings in C.
add the variable to store the number of elements in an array. It is similar to Pascal-strings.
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 10
char array[ARRAY_SIZE + 1];
int array_len(char * inp_arr) {
int ret_val = 0;
while (inp_arr[ret_val] != '\0')
++ret_val;
return ret_val;
}
float array_with_level[ARRAY_SIZE];
int array_with_level_level;
int main() {
array[0] = '\0';
memcpy(array, "hello!\0", 7); // 7'th element is 0
printf("array with 0 at the end\n");
printf("%s, length is %d\n", array, array_len(array));
array_with_level_level = 0;
const int fill_level = 5;
int iter;
for (iter = 0; iter < fill_level; ++iter) {
array_with_level[iter] = iter*iter/2.0;
}
array_with_level_level = iter;
printf("array with length in the dedicated variable\n");
for (int i1 = 0; i1 < array_with_level_level; ++i1)
printf("%02d:%02.2f ", i1, array_with_level[i1]);
printf(", length is %d", array_with_level_level);
return 0;
}
<conio.h> is a non-standard header. I assume you're using Turbo C/C++ because it's part of your course. Turbo C/C++ is a terrible implementation (in 2020) and the only known reason to use it is because your lecturer made you!
However everything you actually use here is standard. I believe you can remove it.
printf("%c",arr); doesn't make sense. arr will be passed as a pointer (to the first character in the array) but %c expects a character value. I'm not sure what you want that line to do but it doesn't look useful - you've listed the array in the for-loop.
I suggest you remove it. If you do don't worry about a \0. You only need that if you want to treat arr as a string but in the code you're handling it quite validly as an array of 10 characters without calling any functions that expect a string. That's when it needs to contain a 0 terminator.
Also add return 0; to the end of main(). It means 'execution successful' and is required to be conformant.
With those 3 changes an input of ABCDEFGHIJ produces:
Alphabet 1:
Alphabet 2:
Alphabet 3:
Alphabet 4:
Alphabet 5:
Alphabet 6:
Alphabet 7:
Alphabet 8:
Alphabet 9:
Alphabet 10:-----------------------------------------List of alphabets: A B C D E F G H I J
It's not pretty but that's what you asked for and it at least shows you've successfully read in the letters. You may want to tidy it up...
Remove printf("\nAlphabet %d:", count); and insert printf("\nAlphabet %d: %c", count,arr[iterator]); after scanf(" %c", &arr[iterator]);.
Put a newline before and after the line of minus signs (printf("\n-----------------------------------------\n"); and it looks better to me.
But that's just cosmetics. It's up to you.
There's a number of ways to find the most frequent character. But at this level I recommend a simple nested loop.
Here's a function that finds the most common character (rather than the count of the most common character) and if there's a tie (two characters with the same count) it returns the one that appears first.
char findCommonest(const char* arr){
char commonest='#'; //Arbitrary Bad value!
int high_count=0;
for(int ch=0;ch<10;++ch){
const char counting=arr[ch];
int count=0;
for(int c=0;c<10;++c){
if(arr[c]==counting){
++count;
}
}
if(count>high_count){
high_count=count;
commonest=counting;
}
}
return commonest;
}
It's not very efficient and you might like to put some printfs in to see why!
But I think it's at your level of expertise to understand. Eventually.
Here's a version that unit-tests that function. Never write code without a unit test battery of some kind. It might look like chore but it'll help debug your code.
https://ideone.com/DVy7Cn
Footnote: I've made minimal changes to your code. There's comments with some good advice that you shouldn't hardcode the array size as 10 and certainly not litter the code with that value (e.g. #define ALPHABET_LIST_SIZE (10) at the top).
I have used const but that may be something you haven't yet met. If you don't understand it and don't want to learn it, remove it.
The terms of your course will forbid plagiarism. You may not cut and paste my code into yours. You are obliged to understand the ideas and implement it yourself. My code is very inefficient. You might want to do something about that!
The only run-time problem I see in your code is this statement:
printf("%c",arr);
Is wrong. At this point in your program, arr is an array of char, not a single char as expected by the format specifier %c. For this to work, the printf() needs to be expanded to:
printf("%c%c%c%c%c%c%c%c%c%c\n",
arr[0],arr[1],arr[2],arr[3],arr[4],
arr[5],arr[6],arr[7],arr[8],arr[9]);
Or: treat arr as a string rather than just a char array. Declare arr as `char arr[11] = {0};//extra space for null termination
printf("%s\n", arr);//to print the string
Regarding this part of your stated objective:
"I shall also have to find the number of a certain element and print it on the screen. I'm new to this. Please help me out."
The steps below are offered to modify the following work
int findTotal(){
}
Change prototype to:
int FindTotal(char *arr);
count each occurrence of unique element in array (How to reference)
Adapt above reference to use printf and formatting to match your stated output. (How to reference)
I am trying to create a formatted string , however I do not know why I cannot print global array which I have modified inside the function.Also the strange behavior is that I cannot access only a specific global array (rand_session_key) rest of the other global arrays are behaving as normal(similar operations are being done on them except their size varies) and I can access their value properly. This code is run on an esp32 (DOIT Dev Kit V1) (with Arduino-Core) , when I run this program on my computer (modifying a few functions etc.) the result is what I expect , I think I am overlapping the characters in the memory or accessing it the wrong way , but had it been the case I would not have yielded the expected output on my computer.
I tried to modify my program and made it more verbose. Also I ran the same code (with some obvious modifications to make it run on my computer) , and the result is good as expected.
char persistent_peripheral_id[] = "FRUCTOSE96";
char rand_session_iden[7] = {'\0'};
char rand_session_key[17] = {'\0'};
char rand_session_channel[3] = {'\0'};
char *generate_random_session_identifier(char *rand_session_iden_local)
{
srand(time(NULL));
int counter = 0;
for (counter = 0; counter < 6; counter++)
*(rand_session_iden_local + counter) = (random(10) % ('~' - ' ')) + 'k';
rand_session_iden_local[counter] = '\0';
printf("Identifier : %s\n", rand_session_iden); //acessing global defintion of array everything is good until here
return &rand_session_iden_local[0];
}
char *generate_random_session_key(char *rand_session_key_local)
{
srand(time(NULL));
int counter = 0;
for (counter = 0; counter < 16; counter++)
*(rand_session_key_local + counter) = (random(10) % ('~' - ' ')) + 'b';
rand_session_key_local[counter] = '\0';
printf("Key : %s\n", rand_session_key);//acessing global defintion of array everything is good until here
return &rand_session_key_local[0];
}
char *generate_random_session_channel(char *rand_session_channel_local)
{
srand(time(NULL));
int channel_value = random(100);
sprintf(rand_session_channel_local, "%03ld", channel_value);
printf("Channel : %s\n", rand_session_channel);//acessing global defintion of array everything is good until here
return &rand_session_channel_local[0];
}
void begin_exchange_package()
{
//If this does not works here (observe rand_session_key) , it will not work for sprintf also ??
printf("\n %s-%s-%s-%s \n", (char *)persistent_peripheral_id,
generate_random_session_identifier(rand_session_iden),
generate_random_session_key(rand_session_key),
generate_random_session_channel(rand_session_channel));
//Notice it prints here ????
printf("\n %s \n",generate_random_session_key(rand_session_key));
Serial.println("Done");
//sprintf((char *)plain_text_package, "{\"p\":\"%s\",\"r\":\"%s\",\"k\":\"%s\",\"c\":\"%s\"}", (char *)persistent_peripheral_id,(char *)rand_session_iden, (char *)rand_session_key , (char *)rand_session_channel);
}
void setup()
{
Serial.begin(115200);
begin_exchange_package();
}
void loop()
{
}
The Output is
FRUCTOSE96-tnltkp--094
Where I expected all the 4 arrays to be printed ?? but it does print separately , is my array being terminated in the wrong way ?? also the logic to assign a random character will always yield a printable ASCII Character (I learned this from a forum on esp32's website)
This code ...
sprintf(rand_session_channel_local, "%03ld", channel_value);
... requires rand_session_channel_local to point to an array of at least four characters, because at will print at least three digits plus a string terminator. The array into which it points, rand_session_channel, is only three characters long. The resulting behavior is undefined.
The observed manifestation of the UB is consistent with the global arrays being laid out in memory such that rand_session_key immediately follows rand_session_channel, such that overflowing the latter means that the string terminator is written to position 0 of the former, making it an empty string. Note, however, that you cannot rely on predicting manifestations of UB, nor is it generally of much use to analyze them. Instead, avoid exercising UB.
It's unclear what random function you are using, since the C standard library's does not take an argument, but if the argument to yours specifies an exclusive upper bound then you could just change the sprintf format to "%02ld". Alternatively, increase the size of rand_session_channel to at least 4.
I am working on a code that gives the steps to change one string into another and it works fine but gives garbage values.
The code is:
void steps(char str1[],char str2[]) {
int belongs,req;
strlwr(str1);
strlwr(str2);
if(strlen(str1)==strlen(str2)) {
for(int i=0;i<=strlen(str1);i++) {
if(str2[i]!=str1[i]) {
++changes;
printf("%d::SUBSTITUTION:: %c <--- %c\n",changes,str1[i],str2[i]);
str1[i]=str2[i];
printf("--->%s\n",str1);
}
}
}
if(strlen(str1)>strlen(str2)){
for(int i=strlen(str1);i>=strlen(str2);i--) {
if(str1[i]=='\0') {
}
else {
++changes;
printf("%d::DELETE:: %c\n",changes,str1[i]);
str1[i]=0;
printf("--->%s\n",str1);
}
}
}
if(strlen(str1)<strlen(str2)) {
for(int i=(strlen(str1));i<=(strlen(str2)-1);i++) {
++changes;
printf("%d::ADD:: %c\n",changes,str2[i]);
str1[i]=str2[i];
printf("-->%s\n",str1);
}
}
steps(str1,str2);
}
The output for the input strings, suppose 'sym' and 'symbiosis' is:
1::ADD:: b
-->symb
2::ADD:: i
-->symbi
3::ADD:: o
-->symbio
4::ADD:: s
-->symbios
5::ADD:: i
*-->symbiosiDzb
6::ADD:: s
-->symbiosis²b
7::DELETE:: b
--->symbiosis²
8::DELETE:: ²
--->symbiosis*
At step 5 and beyond, why is it showing me garbage values?
I tried the best I could and due to my limited knowledge in C (and
pointers) I am reluctant to use pointers and hence hesitate a bit
with DMA. Also, a strange thing that I noticed is that the garbage
values occur only when the difference between the strings is large.
Please help me fix it!
if(strlen(str1)<strlen(str2)){
for(int i=(strlen(str1));i<=(strlen(str2)-1);i++){
++changes;
printf("%d::ADD:: %c\n",changes,str2[i]);
str1[i]=str2[i];
printf("-->%s\n",str1);
}}
In that snippet, the part where you do str1[i]=str2[i]; is probably illegal.
First, strings in C are nullterminated. That means a string ends when a nullbyte comes. So if you add a character, you are overwriting the nullbyte. Therefore, you have to make sure that the character following it is a nullbyte, otherwise the string ends when the next nullbyte comes in memory. That's where the garbage data comes from.
So it would be:
str1[i]=str2[i];
str1[i + 1]=0;
Additionally, you have to make sure that the buffer is large enough. That means that the memory you reserved for str1 when calling steps might not be enough for you to append characters. Technically you can still add new characters, but what you are probably doing is overflowing the buffer and writing to memory that you don't 'own'. Which could be critical.
What you could do is allocate a new buffer with enough memory which holds your new string. You can then copy str1 into it and then append the characters from str2.
For example like this:
char buffer[512];
buffer[0] = 0;
// Write str1 into the buffer
strcat(buffer, str1);
// [...]
// Instead of 'str1[i]=str2[i];'
buffer[i]=str2[i];
buffer[i + 1] = 0;
In this example, you still have to make sure that both str1 and str2 are smaller than 512 bytes though, otherwhise you have to increase the buffer or allocate buffer on the heap using malloc (and free afterwards).
To provide a better example on how to do it, it might be helpful to see how you call the steps function.
Thanks everybody!
I was able to resolve the problem...
The working code is:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int changes=0;
void steps(char str1[],char str2[]){
char* temp;
temp=(char *)malloc((strlen(str2)));//Buffer of size of string 2 to store string 1
unsigned int length_1=strlen(str1);
unsigned int length_2=strlen(str2);
int belongs,req;
strlwr(str1);
strlwr(str2);//To avoid conversion of same letters but in different cases
//Strings of equal length:
if(strlen(str1)==strlen(str2)){
for(int i=0;i<=strlen(str1);i++){
//Strings with no swapping possible:
if(str2[i]!=str1[i]){
++changes;
printf("%d::SUBSTITUTION:: %c <--- %c\n",changes,str1[i],str2[i]);
str1[i]=str2[i];
printf("--->%s\n",str1);
}}}
//String 1 is longer than string 2, hence deletion:
if(strlen(str1)>strlen(str2)){
for(int i=strlen(str1);i>=strlen(str2);i--){
++changes;
printf("%d::DELETE:: %c\n",changes,str1[i]);
str1[i]=0;
printf("--->%s\n",str1);
}
}
//String 2 is longer than string 1, hence addition:
if(strlen(str1)<strlen(str2)){
strcpy(temp,str1);
for(int i=(strlen(str1));i<strlen(str2);i++){
if (str2[i]=='\0'){
}
else{
++changes;
printf("%d::ADD:: %c\n",changes,str2[i]);
*(temp+i)=str2[i];
*(temp+1+i)=0;
printf("-->%s\n",temp);
}}
char newstr[length_2];//Use to store 'temp' and use in steps function again
strcpy(newstr,temp);
steps(newstr,str2);
}
free(temp); //Release memory
}//End!
This is the first part of the program and I have a few questions on how parts of it work exactly. Keep in mind this is the first C program I have written. scanf("%d",&numberOfTimes); Why do I need the & and what does it do?
char input[][200]; Is this basically an array of strings or is it something completely different?
#include <stdio.h>
#include <string.h>
char outputs[100];
char input[][200];
int numberOfTimes;
void io(void){
scanf("%d", &numberOfTimes);
for(int i = 0; i < numberOfTimes; i++){
scanf("%s",input[i]);
}
}
This next part of the code is my attempt at actually solving the problem however I suspect that I screwed up the use of a function but I don't know which one or I used something improperly in order to get this result. (I provided example i/o of me code at the bottom).
void stringManipulation(char string[200]){
int strLength = strlen(string);
int number = strLength/2;
for(int i = 0; i <= number; i=i+2){
strcat(outputs,&string[i]);
}
}
int main(void) {
io();
for(int i = 0; i < numberOfTimes; i++) {
stringManipulation(input[i]);
printf("%s\n",outputs);
memset(&outputs[0], 0, sizeof(outputs));
}
return 0;
}
Did I use memset properly? Again I don't understand the use of the &.
Example input:
4
your
progress
is
noticeable
Expected output:
y
po
i
ntc
Output I am getting:
yourur
progressogressress
is
noticeableticeableceable
Thank you for your help.
The & before a variable means that you are referring to the address of the variable, and not the variable value itself. If you don't know about what the address of a variable is : http://www.cquestions.com/2010/02/address-of-variable-in-c.html
char input[][200] is an array of char array (a char array is vulgarized as a string but it ISN'T the TYPE).
Your problem is about your strcat use, you're adding characters that are between string[i] (included) and string[strLength], not only the string[i] character.
Your problem is in strcat:
Your code passes the address and hence you are getting the entire string from that character onward.
strcat(outputs,&string[i]);
Now, change the above code as below:
strcat(output,string[i]);
You'll get the desired output. The problem with your initial code was that you were passing the address and not individual character.
Also, change the for loop in such a way that "<"number and not "<="number.
Let me know if you have any more doubts.