How do I check if there is an element in a Matrix? - c

I'm Trying to check in my matrix of dimension [10][10], which spots are available to store data (String) there and which are occupied.
The code basically goes through the whole matrix and checks every spot.
I have tried using the strlen and != NULL but everything just prints that the spot is free.
char parque[10][10];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
parque[i][j] = "";
}
}
parque[5][5]="f47ac10b-58cb-4372-a567-0e02b2c3d499,ANR";
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(parque[i][j] != "") {
printf("The Spot [%d][%d] is taken",i,j);
} else {
printf("The Spot [%d][%d] is free",i,j);
}
}
}
Basically the spot [5][5] should print that it's taken, at least that's what I want it to do...
Thanks in advance!

Your declaration
char parque[10][10];
declares a two-dimensional array of char. If you compile your code with a strict compiler, you'll get an error:
error: assignment makes integer from pointer without a cast [-Wint-conversion]
parque[i][j] = "";
^
What you did mean is to make an array of pointers to const char, like here:
const char* parque[10][10];
Then your program will say that The Spot [5][5] is taken.

You can't use !=. You need to use strcmp. And, of course, you need to initialize your array content before iterating it and using its values to compare with "" string.
This condition:
if(parque[i][j] != "")
Will become:
if (strcmp(parque[i][j], ""))

Related

segmentation fault (core dumped) error when trying to copy from an array

trying to copy stuff from b into a but i get that error
someone told me it means i'm trying to access memory that i'm not allowed to, but i don't know what should i do to make it compile.
replace(txt , code);
string replace(string a , string b)
{
string alpha[26] = {"abcdefghijklmnopqurstuvwxyz"};
for (int i = 0; i < strlen(a); i++)
{
for(int n = 0; n < 26; n++)
{
if(a[i] == alpha[n])
{
a[i] = b[n];
i++;
}
}
}
return word;
}
i'm a beginner so no comments about clean coding or syntactic sugar or stuff like that just help me resolve this please
It looks like you have some problems with understending pointers, so I recommend you to read about them. Also consider reading about datatypes and types from STL you are using. (cause std::string is already an array of values so, when you are creating std::string[26], you actually are creating pointer to a pointer)
I guess you have are trying to do something like that:
std::string replace(string a , string b)
{
std::string alpha = {"abcdefghijklmnopqurstuvwxyz"};
for (size_t i = 0; i < a.size(); ++i)
{
for(size_t n = 0; n < alpha.size(); ++n)
{
if(a[i] == alpha[n])
{
a[i] = b[n];
i++; // Also, I think you doesnt need this line, cause you already incrementing i in for loop
}
}
}
return a;
}
Also you have used strlen() on your string, that also is not really correct, cause it is used on char values. If you whant to get length of a string it is better to use string.lenght()
Also, It is better to use size_t or unsigned int instead of int in this case, cause you don't need negative numbers in order to parce these strings. ()

I'm having a problem with sending a char pointer to a function and assigning a value to it in C

I am trying to register to the address I sent to the function in the code structure you see below. But the for loop itself adds something. It only does this on arrays of 11 elements. That is, if "Hasan Polat" comes, he adds it, but if "Hasann Polat" comes, he does not add it. Or anything other than any 11 element array.printf output is written next to it
void sezarSifreleme(char cumle[], int kaydirma, char *sifreliCumle) {
char alfabe[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int i,j, cumleUzunluk = strlen(cumle);
for(i=0; i < cumleUzunluk; i++) {
if(cumle[i]==' ') {
cumle[i] = ' ';
} else {
for(j=0;j < 26; j++){
if(cumle[i] == alfabe[j]){
j = j + kaydirma;
j = j % 26; // Sezar sifrelemede sona geldikten sonra basa donulur onu saglamak amacli 26'ya mod aliriz.
cumle[i] = alfabe[j];
break;
}
}
}
printf("**");
// Sonucu main fonk icerisine gondermek icin aldigimiz degisken adresine sifreledigimiz cumleyi atiyoruz
for(i=0; i < cumleUzunluk; i++) {
sifreliCumle[i] = cumle[i];
printf("%c", sifreliCumle[i]); // **mfxfs utqfy**
}
printf("**");
printf("??%s??", sifreliCumle); // ??mfxfs utqfy??
}
Taking into account these two outputs: one with the fixed number of characters cumleUzunluk in the for loop
printf("**");
// Sonucu main fonk icerisine gondermek icin aldigimiz degisken adresine sifreledigimiz cumleyi atiyoruz
for(i=0; i < cumleUzunluk; i++) {
sifreliCumle[i] = cumle[i];
printf("%c", sifreliCumle[i]); // **mfxfs utqfy**
}
printf("**");
and other with using the conversion specifier s
printf("??%s??", sifreliCumle); // ??mfxfs utqfy??
it means that at least the pointer sifreliCumle does not point to a string.
You could rewrite this call of printf the following way
printf("??%.*s??", cumleUzunluk, sifreliCumle);
or append the zero character '\0' provided that the array pointed to by the pointer sifreliCumle have a space to store the zero character '\0' as for example
sifreliCumle[cumleUzunluk] = '\0';
printf("??%s??", sifreliCumle);
I don't know what your code is doing, but I see that you have this routine:
void sezarSifreleme(char cumle[], int kaydirma, char *sifreliCumle)
and it is defined as having a void return. I see you maninpulating the pointers inside the routine, but none are remained modified outside the routine unless you either use pointer to pointers are you return the pointer in function signature.
char * sezarSifreleme(char cumle[], int kaydirma, char *sifreliCumle)
...
return sifreliCumle;
What is that anyway? Signed Integer, free, li cumulative, little endian?

Copying a certain number of characters from one pointer to another

I have a source pointer (pSource) and a goal pointer (pGoal). I also have a number of characters (n) that need to be copied to the pGoal from pSource.
I thought that I can just copy what's in the pSource to pGoal and move both pointers to the next location. (Both are pointing at the start at the beginning).
for (int i = 0; i < n; i++) {
pGoal+i = pSource+i;
}
Assuming that your pointers are of type char *, the correct way of doing this is:
for (int i = 0; i < n; i++) {
*(pGoal+i) = *(pSource+i);
// or pGoal[i] = pSource[i]
}
You can also check memcpy

Concat LPSTR in C

Trying to use as basic C as I can to build a list of numbers from 1-52 in a random order (deck of cards). Everything works, but all of my attempts to concat the strings and get a result end in failure. Any suggestions? NOTE: This is not homework it's something I'm using to create a game.
// Locals
char result[200] = ""; // Result
int card[52]; // Array of cards
srand(time(0)); // Initialize seed "randomly"
// Build
for (int i=0; i<52; i++) {
card[i] = i; // fill the array in order
}
// Shuffle cards
for (int i=0; i<(52-1); i++) {
int r = i + (rand() % (52-i));
int temp = card[i]; card[i] = card[r]; card[r] = temp;
}
// Build result
for (int c=0; c<52; c++) {
// Build
sprintf(result, "%s%d", result, card[c]);
// Comma?
if ( c < 51 )
{
sprintf(result, "%s%s", result, ",");
}
}
My end result is always garbled text. Thanks for the help.
You keep writing to the same position of "result".
sprintf is not going to do the appending for you.
You may consider, after each sprintf, get the return value (which is the number of char written), and increment the pointer to result buffer. i.e. something like:
(psuedo code):
char result[200];
char * outputPtr = result;
for (int c=0; c<52; c++) {
// Build
int n = sprintf(outputPtr, "%d%s", card[c], (c<51 ? "," : ""));
outputPtr += n;
}
Are we writing C++ or C? In C++, concat-ing a string is just:
string_out = string_a + string_b
…since you'd be using std::string.
Furthermore, if this is C++, the STL has a std::shuffle function.
If this is C, note that all your sprintfs aren't concatenating strings, they're just overwriting the old value.
I think, if memory serves, that sprintf will always write into the buffer starting at byte 0. This means that you would be writing the first couple of bytes over and over again with a number, then a comma, then a number. Check if your first bytes are ",[0-9]" - if so, that's your issue.
This would add a comma between each number in the result string:
// Get a pointer to the result string
char* ptr = &result[0];
for (int c = 0; c < 52; c++) {
// Add each cards number and increment the pointer to next position
ptr += sprintf(ptr, "%d", card[c]);
// Add a separator between each number
if (c < 51) {
*ptr++ = ',';
}
}
// Make sure the result string is null-terminated
*ptr = 0;

is there a function that lets me look at the next char?

is there a function in c that lets me look at the next char in an array? Also where could I find this information on my own, I tried Google and looking for existing threads on this site.
I am trying to pull numbers from a line, and store those numbers. So I want to do something like
if(c = a number and c "next character" is not a number){value is = value*10+c-'0', store number}
If the current character is array[i], the next character is array[i+1].
You could write a method to do this:
char next_char(char *array, int i, int size){
return (++i) < size ? array[i] : '\0';
}
EDIT: After reading your question something like this may be reasonable.
if(isdigit(array[i]) && !isdigit(next_char(array,i,size)){
..
}
A better solution would be a for loop:
int val = 0;
for(i = 0; i < size; i++){
if(isdigit(i)){
val = 10 * val + array[i] - '0';
}else{
// Store the value
val = 0;
}
}
To get a char from a char array, you just use:
array[i]
where is is an index.
If you want to read every char, you can use a loop:
for(size_t i = 0; i < sizeof(array); i++)
{
char cur = array[i];
}
You could also increment a pointer.

Resources