Pointer to Pointer to Char Array - c

My function passwords takes char** as input. I need the function to place a specific char at a location. My program crashes and I'm not sure what I'm doing wrong but I've narrowed it down to the pointer:
// The function
int passwords(int num, int line, char** letters, char** ptrPassword, int length) {
char* password = *ptrPassword;
// Later in the code
password[location] = letters[line][0];
}
Here is my call from main:
char** password;
password = malloc(length * sizeof(char));
location = length;
//call function
printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, password, length));
I'm not very experience with pointers, could someone please assist?
Main:
#include<stdio.h>
#include<stdlib.h>
int passwords(int num, int line, char** letters, char** ptrPassword, int length);
int lengthOfString(char* string);
void print(char** letters, int length);
int location;
int main(){
int numCase;
scanf("%d", &numCase);
int i, length;
for(i = 0; i < numCase; i++){
scanf("%d", &length);
int j;
char** letters = malloc(length*sizeof(char*));
for(j = 0; j < length; j++){
letters[j] = calloc(26, sizeof(char));
scanf("%s", letters[j]);
}
int rank;
scanf("%d", &rank);
//print(letters, j);
char* password;
password = malloc(length * sizeof(char));
location = length;
//call recursion
printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length));
}
return 0;
}
The Entire Function:
//recursive function
int passwords(int num, int line, char** letters, char** ptrPassword, int length){
char* password = *ptrPassword;
printf("Recursion #%d \n", line);
if(line == length-1){
printf("Line is equal to length \n");
if(num > lengthOfString(letters[line])){
printf("IF \n");
if(num % lengthOfString(letters[line]) == 0){
password[location] = letters[line][lengthOfString(letters[line])];
}
else{
password[location] = letters[line][num % lengthOfString(letters[line]) - 1];
}
printf("location: %d \n", location);
location--;
printf("Password is: %s \n", password);
}
else{
printf("ELSE \n");
if(num / lengthOfString(letters[line]) == 1){
*password[location] = letters[line][0];
}
else{
printf("Alocation: %d \n", location);
password[location] = letters[line][num / lengthOfString(letters[line])];
}
printf("Blocation: %d \n", location);
location--;
printf("Password is: %s \n", password);
}
return lengthOfString(letters[line]);
}
else{
printf("Line is not equal to length \n");
int scalar = passwords(num, ++line, letters, ptrPassword, length);
if (num > scalar){
if(num % scalar == 0){
password[location] = letters[line][lengthOfString(letters[line])];
}
else{
password[location] = letters[line][num % scalar - 1];
}
location--;
}
else{
if(num / scalar == 1){
password[location] = letters[line][0];
}
else{
password[location] = letters[line][num / lengthOfString(letters[line])];
}
location--;
}
return scalar * lengthOfString(letters[line]);
}
}

Change this line
printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length));
to
printf("Password at %d is %d \n", rank, passwords(rank, 0, letters, &password, length));
Since your passwords function returns an integer, not a string (char *). The same is evident from the compiler warning as well.

Related

Why my fputs doesn't work when the file already contains anything?

I try to add a new line to the already existing file, which already consists some text. I found out that when the file contains something, the program stops working after fputs(stringToAdd, myFile); in function void Save(struct SEED arrayToSave[], int size). However, when I try to write in an empty file, everything works properly. Can you guys please help me?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int numberOfItems = 0;
char path[] = "Catalogue.txt";
char text[1000000];
struct SEED {
int index;
char type[30];
char name[60];
float mass;
float price;
};
void GetText() {
FILE* catalogueFile;
char ch; // temporary char
int counter = 0;
catalogueFile = fopen(path, "r");
while((ch = fgetc(catalogueFile)) != EOF ) {
text[counter] = ch;
counter++;
}
fclose(catalogueFile);
}
void CalculateNumberOfItems() {
for(int i = 0; i < strlen(text); i++) {
if(text[i] == '\n') numberOfItems++;
}
}
struct SEED* GetArrayOfStructs() {
GetText();
CalculateNumberOfItems();
FILE* catalogueFile;
int counter = 0;
char* arrayOfSubstrings[numberOfItems];
struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof(arrayOfStructs));
struct SEED* arrayOfStructsPointer = arrayOfStructs;
char* token = strtok(text, "\n");
while( token != NULL ) {
arrayOfSubstrings[counter] = token;
token = strtok(NULL, "\n");
counter++;
}
for(int i = 0; i < numberOfItems; i++) {
struct SEED tempStruct;
sscanf(arrayOfSubstrings[i], "%d %s %s %f %f", &tempStruct.index, tempStruct.type, tempStruct.name, &tempStruct.mass, &tempStruct.price);
arrayOfStructs[i] = tempStruct;
}
return arrayOfStructsPointer;
}
void Save(struct SEED arrayToSave[], int size) {
FILE* myFile = fopen("Catalogue.txt", "w");
char fullString[1000000];
for(int i = 0; i < size; i++) {
struct SEED currentStruct = arrayToSave[i];
char stringToAdd[1000];
sprintf(stringToAdd, "%d %s %s %f %f\n", currentStruct.index, currentStruct.type, currentStruct.name, currentStruct.mass, currentStruct.price);
strcat(fullString, stringToAdd);
}
printf("%s\n", fullString);
fputs(fullString, myFile);
fclose(myFile);
}
void AddNew() {
struct SEED* oldArrayOfStructs = GetArrayOfStructs(path);
struct SEED newArrayOfStructs[numberOfItems + 1];
struct SEED newStruct;
int newIndex = numberOfItems + 1;
char newType[30], newName[60];
float newMass, newPrice;
char tempChar[200];
printf("Input type, name, mass and price\nof the new seed whith spaces: ");
fgets(tempChar, 200, stdin);
fgets(tempChar, 200, stdin);
sscanf(tempChar, "%s %s %f %f\\n", newType, newName, &newMass, &newPrice);
newStruct.index = newIndex;
strcpy(newStruct.type, newType);
strcpy(newStruct.name, newName);
newStruct.mass = newMass;
newStruct.price = newPrice;
printf("%d\n", numberOfItems);
for(int i = 0; i < numberOfItems; i++) {
newArrayOfStructs[i] = oldArrayOfStructs[i];
}
newArrayOfStructs[numberOfItems] = newStruct;
Save(newArrayOfStructs, numberOfItems + 1);
printf("\n");
}
void UserInput() {
char choice;
int choiceInt, a = 1;
printf("Add new item - print 1\n");
printf("\n");
printf("Input your choice: ");
choice = getchar();
choiceInt = choice - '0';
printf("\n");
switch(choiceInt) {
case 1:
AddNew();
break;
default:
printf("Bye!\n");
break;
}
}
int main() {
UserInput();
return 0;
}
At least these problems:
Allocation too small
Code incorrectly allocates for an array of pointers.
// struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof(arrayOfStructs));
struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof arrayOfStructs[0]);
char too small to distinguish 257 different responses from fgetc()
// char ch; // temporary char
int ch;
...
while((ch = fgetc(catalogueFile)) != EOF ) {
Potential buffer overrun
Use a width.
No point scanning for 2 characters \ and n with "\\n".
// sscanf(tempChar, "%s %s %f %f\\n", newType, newName, &newMass, &newPrice);
sscanf(tempChar, "%29s %59s %f %f", newType, newName, &newMass, &newPrice);
Best to test sscanf() result too.
if (sscanf(tempChar, "%29s %59s %f %f", newType, newName, &newMass, &newPrice) != 4) {
TBD_error_code();
}
Overflow risk
// while((ch = fgetc(catalogueFile)) != EOF ) {
while(counter < (sizeof text - 1) && (ch = fgetc(catalogueFile)) != EOF ) {
text[counter] = ch;
Maybe overflow risk
// sprintf(stringToAdd, "%d %s %s %f %f\n",
snprintf(stringToAdd, sizeof stringToAdd, "%d %s %s %f %f\n",

Trying to understand the problems in my c code

I created a code with 4 functions:
Data_duplication that checks if the name is already exists.
Init that the function initializes through input from the user a set of names and a set of scores. Both arrays of the same size which called size. Whenever data is collected for arrays, they must be valid. If the user typed an invalid name, an error message should be printed and an alternate statistic requested. A valid score is complete between 0 and 100. A valid name meets the following conditions:
Begins with a large Latin letter.
All but the first characters are lowercase Latin characters.
Not already in the array. (The array must not contain the same name twice.)
Find function that gets as parameters, an array of names, an array of grades, and the size of these arrays. In addition, it receives as a student name parameter. The function finds the student's position in the array of names, and returns its grade. If the student does not appear in the set, 1- will be returned.
FreeAll that frees all the memory from the arrays.
PROBLEM:
When i enter for example that there are 3 students it is asking from me to write a fourth student and then it writes a grade that I don't know from where and gets out of the program.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define SIZE 20
int Data_duplication(char* temp, char** names, int line);
void Init(char*** names, int** grades, int* size);
int Find(char** names, int* grades, int size, char* name);
void FreeAll(char*** names, int** grades, int size);
int main()
{
char** Names = NULL;
int* Grades = NULL;
int size, grade;
char name[SIZE] = { 0 };
Init(&Names, &Grades, &size);
printf("Enter a student name\n");
scanf("%s", name);
grade = Find(Names, Grades, size, name);
printf("%d", grade);
FreeAll(&Names, &Grades, size);
return 0;
}
void Init(char*** names, int** grades, int* size)
{
int i, j, flag;
int strlengh;
char temp[SIZE] = { 0 };
printf("Enter number of students\n");
scanf("%d", size);
*names = (char**)malloc((*size) * sizeof(char*));
if (!(*names))
{
printf("Error");
return;
}
*grades = (int*)malloc((*size) * sizeof(int));
if (!*grades)
{
printf("Error");
return;
}
for (i = 0; i < *size; i++)
{
printf("Enter a name\n");
scanf("%s", temp);
strlengh = strlen(temp);
do
{
flag = 1;
if (strlen(temp) > 20)//if it longer then it should be
flag = 0;
if (temp[0] > 'Z' || temp[0] < 'A') //start with capital letter
flag = 0;
for (j = 1; temp[j] != '\0'; j++)//all the letter is a lower case letters except from the first
{
if (temp[j] > 'z' || temp[j] < 'a')
{
flag = 0;
break;
}
}
if (Data_duplication(temp, *names, i))//if its not a name that already entered
{
flag = 0;
}
if (flag)//if the name is ok
{
(*names)[i] = (char*)malloc((strlengh + 1) * sizeof(char));
if (!(*names)[i])
{
printf("Error");
return;
}
strcpy((*names)[i], temp);
}
else//if somthing wrong
{
printf("Bad name,try again.\n");
scanf("%s", temp);
}
} while (!flag);
printf("Enter grade\n");
scanf("%d", (*grades + i));
while (*(*grades + i) < 0 || *(*grades + i) > 100)//if the grade between 0 to 100
{
printf("Bad grade,try again.\n");
scanf("%d", (*grades + i));
}
}
}
int Data_duplication(char* temp, char** names, int line)//find if there is another name like this that already entered
{
for (int i = 0; i < line; i++)
{
if (!strcmp(temp, names[i]))
{
return 1;
}
}
return 0;
}
int Find(char** names, int* grades, int size, char* name)
{
int i;
for (i = 0; i < size; i++)
{
if (strcmp(name, names[i]) == 0);
{
return (*(grades + i));
}
}
return -1;
}
void FreeAll(char*** names, int** grades, int size)//free al the dynamic memo allocation
{
for (int i = 0; i < size; i++)
{
free(*(*names + i));
}
free(*names);
free(*grades);
}
arrays passed by reference so you don't need to pass pointer to array.
I made changes in your code.
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<string.h>
#define SIZE 20
int Data_duplication(char* temp, char** names, int line);
void Init(char** names, int* grades, int size);
int Find(char** names, int* grades, int size, char* name);
void FreeAll(char** names, int* grades, int size);
int main()
{
int size, grade;
char name[SIZE];
printf("Enter number of students\n");
scanf("%d", &size);
char** Names=(char**)malloc((size) * sizeof(char*));
int* Grades= (int*)malloc((size) * sizeof(int));
Init(Names, Grades, size);
printf("--------------------------------------------------------------------\n");
printf("Enter a student name\n");
scanf("%s",name);
grade = Find(Names,Grades,size,name);
printf("%d", grade);
FreeAll(Names, Grades, size);
return 0;
}
void Init(char** names, int* grades, int size)
{
int i, j, flag;
int strlengh;
if (!(names))
{
printf("Error");
return;
}
for(i = 0; i < size ; i++){
names[i] = malloc(sizeof(char)*SIZE);
if(!names[i])
{
printf("Error");
return;
}
}
if (!grades)
{
printf("Error");
return;
}
for (i = 0; i < size; i++)
{
printf("Student i= %d\n",i);
do
{
char temp[SIZE]="";
printf("Enter a name\n");
scanf("%s", temp);
strlengh = strlen(temp);
flag = 1;
if (strlen(temp) > SIZE)//if it longer then it should be
flag = 0;
if (temp[0] > 'Z' || temp[0] < 'A') //start with capital letter
flag = 0;
for (j = 1; temp[j] != '\0'; j++)//all the letter is a lower case letters except from the first
{
if (temp[j] > 'z' || temp[j] < 'a')
{
flag = 0;
break;
}
}
if (Data_duplication(temp, names, i))//if its not a name that already entered
{
flag = 0;
}
if (flag)//if the name is ok
{
strcpy(names[i], temp);
printf("temp= %s\n", temp);
printf("names[i]= %s\n", names[i]);
}
else//if somthing wrong
{
printf("Bad name,try again.\n");
}
} while (!flag);
printf("Enter grade\n");
scanf("%d",&grades[i]);
while (grades[i] < 0 || grades[i] > 100)//if the grade between 0 to 100
{
printf("Bad grade,try again.\n");
scanf("%d",&grades[i]);
}
}
}
int Data_duplication(char* temp, char** names, int line)//find if there is another name like this that already entered
{
for (int i = 0; i < line; i++)
{
if (!strcmp(temp, names[i]))
{
return 1;
}
}
return 0;
}
int Find(char** names, int* grades, int size, char* name)
{
int i;
for (i = 0; i < size; i++)
{
printf("name= %s\n",name);
printf("names[i]= %s\n", names[i]);
if (strcmp(name, names[i]) == 0)
{
return (grades[i]);
}
}
return -1;
}
void FreeAll(char** names, int* grades, int size)//free al the dynamic memo allocation
{
for (int i = 0; i < size; i++)
{
free(names[i]);
}
free(names);
free(grades);
}

Properly buffering and validating string

I am getting a bit of an error in my program, it is somewhat basic but here is my error: I am trying to properly input a series of strings into a structure and it seems like user input is ending at the wrong spot.
#include <stdio.h>
#include <string.h>
#define buffer 256
struct Fisher
{
char SSN[8]; //9 digit max
char First_Name[12];
char Last_Name[15];
char Phone[10];
char Email[35];
}typedef Fisher;
void getFisher(Fisher* pfisher, int i);
void dispFisher(Fisher* pfisher, int i);
void Fisherman_Menu(Fisher* pfisher, int i);
int main()
{
int i = 0;
Fisher fisherarray[3];
Fisher* pfisher = &fisherarray[0];
Fisherman_Menu(pfisher, i);
return 0;
}
void Fisherman_Menu(Fisher* pfisher, int i)
{
for(;;)
{
fflush(stdin);
printf("-1-Register Fisherman\n");
printf("-2-Search Fisherman\n");
printf("-3-Go back to Main Menu\n");
fflush(stdin);
int choice=0;
scanf(" %d", &choice);
if (choice == 1)
{
fflush(stdin);
getFisher(pfisher, i);
i++;
}
if (choice == 2)
{
fflush(stdin);
dispFisher(pfisher, i);
}
else if (choice == 3)
{
break; /* Break out of loop */
}
else
printf("Anything else?\n");
}
/* When this function returns, you get back to the main menu */
}
void getFisher(Fisher* pfisher, int i)
{
char input[buffer];
char* pinput = NULL;
//===============================
printf("Enter Social Security Number: ");
pinput = fgets(input, buffer, stdin);//validate
strcpy((pfisher+i)->SSN, pinput);
//(pfisher+i)->SSN = atoi(pinput);
//==============================
printf("Enter first name: ");
pinput = fgets(input, buffer, stdin);//validates name
strcpy((pfisher+i)->First_Name, pinput);
//==============================
printf("Enter last name: ");
pinput = fgets(input, buffer, stdin);//validates name
strcpy((pfisher+i)->Last_Name, pinput);
//==============================
printf("Enter phone number as a 10 digit number (without any
dashes or spaces): ");
pinput = fgets(input, buffer, stdin);//validate phone
strcpy((pfisher+i)->Phone, pinput);
//(pfisher+i)->Phone = atoi(pinput);
//===============================
printf("Enter email address: ");
pinput = fgets(input, 20, stdin);//validates name
strcpy((pfisher+i)->Email, pinput);
}
void dispFisher(Fisher* pfisher, int i)
{
int len;
int pen;
printf("ssn is equal to: %s\n", ((pfisher+0)->SSN));
printf("First Name is equal to: %s\n", ((pfisher+0)->First_Name));
printf("ssn is equal to: %s\n", ((pfisher+1)->SSN));
printf("First Name is equal to: %s\n", ((pfisher+1)->First_Name));
len = strlen(((pfisher+0)->SSN));
printf("string length = %d\n", len);
pen = strlen(((pfisher+1)->SSN));
printf("string length = %d\n", len);
/*
int arr = 0;
char searchSSN[9];
printf("Enter SSN: ");
scanf("%s", &searchSSN);
for(arr = 0; arr < i; arr++)
{
if(strcmp(&searchSSN, (pfisher+arr)->SSN) == 0)
{
//printf("I is equal to: %s\n", (pfisher+i)->SSN);
printf("Fisher\n");
printf("-------------------------------------------\n");
printf("%d \n %s \n %s \n %s \n %s ", (pfisher + arr)->SSN, ((pfisher+arr)->First_Name), ((pfisher+arr)->Last_Name), ((pfisher+arr)->Phone), (pfisher+arr)->Email);
printf("-------------------------------------------\n");
}
}
*/
}
My output for SSN is getting messed up and becoming "123456789(FirstName string)
example: 123456789TurboTurkey
How can I clean up the length of the string and get in the SSN properly
There is no enough space to store 9 digit in SSN. If you want to store 9 digit in it, its size should be 10 (9 digit + NULL). If there is no NULL charactor, it will read near by locations too..

Error printing an array in C

The error occurs in the line:
printf("\n%s was found at word number(s): %d\n", search_for, pos);
I want to print my array of ints (pos) but I am not sure how to do so. I am running it through the command line and I am getting this error:
search_word.c:57:28: warning: format specifies type 'int' but the argument hastype 'int *' [-Wformat] search_for, pos);
Code:
const int MAX_STRING_LEN = 100;
void Usage(char prog_name[]);
int main(int argc, char* argv[]) {
char search_for[MAX_STRING_LEN];
char current_word[MAX_STRING_LEN];
int scanf_rv;
int loc = 0;
int pos[MAX_STRING_LEN] = {0};
int word_count = 0;
int freq = 0;
/* Check that the user-specified word is on the command line */
if (argc != 2) Usage(argv[0]);
strcpy(search_for, argv[1]);
printf("Enter the text to be searched\n");
scanf_rv = scanf("%s", current_word);
while ( scanf_rv != EOF && strcmp(current_word, search_for) != MAX_STRING_LEN ) {
if (strcmp(current_word, search_for) == 0) {
loc++;
freq++;
pos[loc] = word_count;
}
word_count++;
scanf_rv = scanf("%s", current_word);
}
if (freq == 0)
printf("\n%s was not found in the %d words of input\n",
search_for, word_count);
else
printf("\n%s was found at word number(s): %d\n",
search_for, pos);
printf("The frequency of the word was: %d\n", freq);
return 0;
} /* main */
/* If user-specified word isn't on the command line,
* print a message and quit
*/
void Usage(char prog_name[]) {
fprintf(stderr, "usage: %s <string to search for>\n",
prog_name);
exit(0);
} /* Usage */
pos is an array. You have to print it in a loop.
Do
else {
printf("\n%s was found at word number(s): ",
search_for);
for (int index = 0; index < MAX_STRING_LEN; index++)
printf("%d ", pos[index]);
printf("\n");
}
You'd need to loop over the array. C doesn't have any way to print an array of ints in a single statement:
else {
int i;
printf("\n%s was found at word number(s): ", search_for);
for ( i = 0; i < loc; ++i ) {
if (i > 0)
printf(", ");
printf("%d", pos[i]);
}
printf("\n");
}
Before that, though, make sure you increment loc at the right time. As-is, you're leaving the first element empty.
if (strcmp(current_word, search_for) == 0) {
pos[loc] = word_count;
loc++;
freq++;
}

scanf check in while loop to restrict integer input

I am writing a code asking the user to input 10 integers, which are then fed to him backwards. I would like to create a "scanf check" to restrict character input. The while loop works insofar that it doesn't accept char, but it skips a integer input.
int main()
{
int i = 0, number[10] = {0};
char buf[128] = {0};
for (i = 0; i < 10; i++)
{
printf("Please input number %d : ", i+1);
while(scanf("%d", &number[i]) != 1)
{
scanf("%s", &buf);
printf("Sorry, [%s] is not a number. Please input number %d : ", &buf, i);
}
}
for (i = 0; i < 10; i++)
{
printf("\n Number %d is %d", (10-i), number[9-i]);
}
return EXIT_SUCCESS;
}
As pointed out by H2CO3, don't use scanf, an alternative is fgets and strtol:
int i, number[10] = {0};
char buf[128], *p;
for (i = 0; i < 10; i++) {
printf("Please input number %d : ", i+1);
while (1) {
fgets(buf, sizeof(buf), stdin);
if ((p = strchr(buf, '\n')) != NULL) {
*p = '\0';
}
number[i] = (int)strtol(buf, &p, 10);
if (p == buf || *p != '\0') {
printf("Sorry, [%s] is not a number. Please input number %d : ", buf, i + 1);
} else {
break;
}
}
}
for (i = 0; i < 10; i++) {
printf("\n Number %d is %d", (10-i), number[9-i]);
}
return EXIT_SUCCESS;
The code works fine for integer also. The only mistake I found is while printing the sorry message, you are printing just i, it should be i+1.
int i = 0, number[10] = {0};
char buf[128] = {0};
for (i = 0; i < 10; i++)
{
printf("Please input number %d : ", i+1);
while(scanf("%d", &number[i]) != 1)
{
scanf("%s", &buf);
printf("Sorry, [%s] is not a number. Please input number %d : ", &buf, i+1);
}
}
for (i = 0; i < 10; i++)
{
printf("\n Number %d is %d", (10-i), number[9-i]);
}

Resources