Kenshin
Kenshin

Reputation: 177

How to check each character in a dynamically allocated string array, in C?

So what I am trying to ultimately do is search an array for a name, and if the name is found return that name. Well, to do that I need to check for each character in every row and column for a match. And before I could do that I need to know exactly how to go about doing that, so I am trying to figure out how to get the dynamic array to print out the first character, then second and so on in order to compare it with the name being searched. But I am having trouble doing this. So my question is how would I go about checking each character in such an array? I've let out most parts of the code but I think I included the main parts that I am troubled in. Thanks for the help in advance. I'm a beginner in C, so sorry if I did anything gravely wrong, thanks!

 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #define STRINGSIZE 20    
 int main(){
 char **firstNames, **lastNames;
 int classSize,i;
 char sample[21] = "Slack";
 printf("Please indicate number of records you want to enter (min 5, max 15):\n");
 scanf("%d", &classSize); 
 firstNames=malloc(classSize*sizeof(char*));
 for (i=0; i<classSize; i++) {
   firstNames[i]=malloc(STRINGSIZE*sizeof(char));
 }
 printf("Please input records of students (enter a new line after each record), with following    format: first name");
 *firstNames="Slack";
 printf("\n\n");
 printf("%c", *(sample));    //Will print out S
 printf("%c", **firstNames); //Will print out S
 printf("%c", *(sample+1));    //Will print out l
 printf("%c", **(firstNames+1)); //Will give error
 printf("%c", **(firstNames)+1); //Will print T (Next ascii char after 'S'
 printf("%c", **((firstNames)+1)); //Will give error
 }

Upvotes: 0

Views: 698

Answers (2)

2501
2501

Reputation: 25753

You have an array of pointers to char firstNames the size of classSize. Every pointer in this array points to valid memory you allocated with malloc.

The error you make is assigning a string literal to the first pointer of the array firstNames, overwriting the pointer with the address of the string literal. This will lose the memory you allocated and also the string literal cannot be modified, which you do later causing the program to crash.

This line will copy a string "Slack" to the memory the first pointer in the array points to:

strcpy( firstNames[0] , "Slack" ) ;  //make sure you have enough space

Note that firstNames[0] equals to *firstNames.

i used in the for loop is not defined anywhere.

Upvotes: 1

user1342784
user1342784

Reputation:

A C string is an array of characters. Even if dynamically allocated, you can treat it as such, so:

sample[0]; // S
sample[1]; // l
sample[2]; // a
// etc

You are storing multiple pointers to C strings in firstNames. You access it as an array:

firstNames[0]; // first name
firstNames[1]; // second name
firstNames[2]; // third name
// etc.

Now you just combine these, as firstName[0] is just a C string, just like sample:

firstName[0][0]; // first letter in first name
firstName[0][1]; // second letter in first name
firstName[1][0]; // first letter in second na,e
// etc.

Upvotes: 1

Related Questions