2

I try to put value of two integers into char array. If I have:

x = 3;
y = 5;

then I want to have

links[0][0] = "3,5";

I have this code now, but I don't know how can I continue.

char** links = (char**) calloc(SRTM_SIZE, sizeof(char*));
if(links)
{
    for(int i = 0; i < SRTM_SIZE; i++)
    {
        links[i] = (char*)calloc(SRTM_SIZE, sizeof(char));
        //memset(links[i], 0, sizeof(*links[i] * SRTM_SIZE));
    }
}

x = strtok(temp, ",");
y = strtok(NULL, ",");

int xx = atoi(x);
int yy = atoi(y);

//Some calculation with x and y and if it's okay, then I need to put value of x and y to array, but I don't know how

printf("%s\n", links[0][0]);

edited:

What exactly I need is matrix (propably 1201x1201) of strings. Into cells (not into all, but propably into most of them) I need put string values. This values can be from "0,0" to "1200, 1200". And later in program I need to acces to all cells with strings values, because each value is position of one of adjacent cells.

1
  • How do you plan on using a char's space to store a string of size more than one byte? Commented Dec 29, 2013 at 18:19

4 Answers 4

1

use sprintf

sprintf(links[0][0],"%d,%d",x,y);

but before change links[0][0] to string(char*)

"3,5" is string not a character.

more info http://www.cplusplus.com/reference/cstdio/sprintf/

Sign up to request clarification or add additional context in comments.

4 Comments

Don't you think link[0][0] should be link[0]?
Might have you forget to see >'but before change links[0][0] to string(char*)'
What is this string(char*) type?
In C, String is represented/referred by char*. In this case, link[0] is of char* type but according to requirement of 'asker', link[0][0] must contains string. He needs to change its type by one level.
1

links[0][0] is of single byte. You can store only 1 byte to it ,i.e, a single character. These two characters would store into the two different memory location, say, link[0][0] and link[0][1]. Then you also need a \0 character to use %s to print them.
The statement

printf("%s\n", links[0][0]);  

is wrong %s is used for strings but links[0][0] is a char.
You can do this as

sprintf(links[0], "%d,%d", xx,yy);
printf("%s\n", links[0]);   

Test code after changes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SRTM_SIZE 5

int main(void){
    char** links = (char**) calloc(SRTM_SIZE, sizeof(char*));
    if(links)
    {
        for(int i = 0; i < SRTM_SIZE; i++)
        {
            links[i] = (char*)calloc(SRTM_SIZE, sizeof(char));
            //memset(links[i], 0, sizeof(*links[i] * SRTM_SIZE));
        }
    }

    char temp[5]="2,3";
    char *x = strtok(temp, ","); //strtok() returns pointer to char.
    char *y = strtok(NULL, ",");

    int xx = atoi(x);
    int yy = atoi(y);
    sprintf(links[0], "%d,%d", xx,yy);
    //Some calculation with x and y and if it's okay, then I need to put value of x and y to array, but I don't know how

    printf("%s\n", links[0]);

}

9 Comments

So I need to change type od my array?
I don't try it yet. But if I use your code, will I have in each cell one string? PS I edited my question.
What do you mean by each cell one string ?
Each cell (links[r][c]) contains char[] value from "0,0" to "1200,1200". I need save in each cell position of another cell.
links[r][c can contains only one character. It is links[r] which contains "0,0" or "1200,1200" string.
|
0
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int len;
    int x = 3, y = 5;
    len = snprintf(NULL, 0, "%d,%d", x, y);
    char *link = malloc(len + 1);
    sprintf(link, "%d,%d", x, y);
    printf("%s\n", link);
    free(link);
    return 0;
}

Comments

0

You can't assign a value to a 2D array the way you do it. This is how it works:

arr[0][0]= x;

arr[0][0] refers to a single location, that is row 0, and column 0.

If you have arr[2][2], you will have to specify where you want to put your value in the 2D array. If you want it in the first row, at the last column:

arr[0][2]= x;

You may visualize a 2D array as follows:

int  arr[3][2]:

             column 0     column 1

row 0       |arr[0][0]| |arr[0][1]|
row 1       |arr[1][0]| |arr[0][1]|
row 2       |arr[2][0]| |arr[0][1]| 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.