0

This probably has to do with C pointers cause it's an array problem.

Is it possible to assign an index of one array to another:

int main()
{
    char a[10];
    char b[3] = {'a', 'b', 'c'};
    int num = 1;
    a[1 + num] = b[0];

    int i = 0;
    for(i; i < 3; i++)
    printf("%s", a[i]);

    return 0;
}

I think the output should be what a would be. which i think would be [(null), (null), a]

EDIT

say I add an int array c to the code above:

int c[3] = {1, 2, 3};

I am having trouble with storing values in the array a when I try to do something like this:

a[1 + c[0]] = b[0];

this ended up working as well...

it turns out this was not my issue thanks for the help though

3
  • What is your desired output? Commented Mar 18, 2015 at 4:35
  • 2
    printf("%s", a[i]); shouldn't that be printf("%c", a[i]);? Commented Mar 18, 2015 at 4:36
  • 1
    You'll want to use %c not %s. Also, you should initialize array a otherwise you will be printing two uninitialized characters. Commented Mar 18, 2015 at 4:36

1 Answer 1

3

You can assign like this. In this There may be a junk characters can available in the array. So First you assign 0 to all.

char a[10]={0};

Then assign the value to index which you need.

After that while printing that use the %c in printf.

for(; i < 3; i++)
printf("%c", a[i]);

You can get the value which you assigned.

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

2 Comments

0 and NULL are not the same thing. And NULL is not a valid character.
Even better :for(; i < 3; i++)

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.