accessing the elements of a char* - c

I have a char *p = "abcd", how can I access the elements 'a','b','c','d' using only C(not C++)? Any help would be appreciated .

You can use indexing:
char a = p[0];
char b = p[1];
/* and so on */
Equivalently you can use pointer arithmetic, but I find it less readable:
char a = *p;
char b = *(p+1);
If you really want to surprise someone you can also write this:
char a = 0[p];
char b = 1[p];
/* and so on */

Here, p refers an array of character pointer. You ca use the array indexing to access each variable in that array. The most widely used notation for this is p[n], where n is the n+1th character [element].
example:
for the 1st character, use p[0], 2nd character, use p[1] and so on..

another example:
#include <stdio.h>
int main()
{
char *p="abcd";
for (; *p; p++)
printf("%c\n", *p);
return 0;
}
result is:
a
b
c
d

Use the array subscript operator []. It allows you to access the nth element of a pointer type in the form of p[n].
You can also increment the pointer by using the increment operator ++.

#include <stdio.h>
int main()
{
char *p="abcd";
printf("%c\n", p[0]);
printf("%c\n", p[1]);
printf("%c\n", p[2]);
printf("%c\n", p[3]);
return 0;
}
returns
a
b
c
d

#include <stdio.h>
#include <stdlib.h>
void main()
{
char* str="Hello, World!";
printf("%s\n",str);
}
Output:
Hello, World!
2nd Method:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char* str="Hello";
int n=strlen(str);
for(int i=0;i<n;i++)
{
printf("%c"str[i]);
}
}
Output:
Hello

Related

passing string to a pointer in c

I am fairly new in C. I want to assign string in a function to a pointer but I have no idea why it is not working?
This is the initial code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
void test(char* result) {
*result = "HELLO";
}
int main() {
char result[64];
test(result);
printf("%s", *result);
}
This is the error: warning: assignment makes integer from pointer without a cast. Since * result should store value and result is the address, shouldn't this work out?
Hello and welcome to C.
Your statement:
*result = "HELLO";
is the same as attempting to do the following:
result[0] = "HELLO"
which is attempting to set a single character to a string, and you can't do that.
you will need to copy the string character by character
luckily there is a function for that which you have included already with <string.h> called strcpy
strcpy(result,"HELLO")
This will work as long as your string to copy is fewer than 63 characters as you have defined in your main() function.
char result[64];
you should probably also send the length of the string to the test function and use strncpy
strncpy(result,"HELLO",length); // safe copy
and then terminate the string with '\0'
result[length-1] = 0;
your printf doesn't need to dereference the string pointer. So simply printf("%s",result); is fine.
so in summary:
void test(char* result,uint32_t len) {
strncpy(result,"HELLO",len); // safe copy (however "HELLO" will work for 64 length string fine)
result[len-1] = 0; // terminate the string
}
#define MY_STRING_LENGTH 64
int main() {
char result[MY_STRING_LENGTH ];
test(result,MY_STRING_LENGTH);
printf("%s",result); // remove *
}
You declared an array in main
char result[64];
Passed to the function it is converted to rvalue of the type char * that points to the first element of the array. The function deals with a copy of this pointer. Changing this copy of the pointer fors not influence on the original array.
Within the function the expression *result has the type char. So this assignment
*result = "HELLO";
does not make a sense.
In this call
printf("%s", *result);
there is again used an incorrect expression of the type char *result.
What you need is to use standard string function strcpy.
For example
#include <stdio.h>
#include <string.h>
void test(char* result) {
strcpy( result, "HELLO" );
}
int main( void ) {
char result[64];
test(result);
puts( result );
}
Problem:
When you store a character in a char varible,it puts the ASCII of the character in the memory.
char c='a';is the same aschar c=97;
You can verify this by using the code:
char c='a';
printf("%d",c);
So here is one way:
void test(char* result) {
*result++ = 'H';
*result++ = 'E';
*result++ = 'L';
*result++ = 'L';
*result = 'O';
}
int main() {
char result[64];
test(result);
printf("%s", result);
}
But it is redundant because there is a function called strcpy in <string.h>.
#include <stdio.h>
#include <string.h>
void test(char* result) {
strcpy( resul, "HELLO" );
}
int main() {
char result[64];
test(result);
puts( result );
}
Remove the '*' of the variable "result" after you've declared it and use the function "strcpy()" in your code.

printf doesn't work on specific context, why?

I needed to test something and programmed this little bit of code(shown below). I can't understand why the first print works and the second doesn't. The output of this program is just
this prints
but it should be
this prints
this doesn't print i: 1
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(char *str) {
char *z[1];
strcpy(*z, "z");
int a;
a = strcmp(str, *z);
return a;
}
int main() {
int i;
char *name[1];
printf("this prints\n");
strcpy(*name, "y");
i = cmp(*name);
printf("this doesn't print i:%d", i);
return 0;
}
char *z[1]; // this is an array of pointer
strcpy(*z, "z"); // you have to allocate at least 2 bytes for *z
// and
char *name[1];
strcpy(*name, "y"); // you have to allocate at least 2 bytes for *name also
You did not allocate for the pointer in array z and name.
Your cmp function looks weird. If you want to compare string with "z", you can just do:
int cmp(char *str){
return strcmp(str, "z");
}
You do not need to use char *name[1], just char *name = malloc(SIZE+1); or char name[SIZE+1] (SIZE is length of string that you want to compare) is enough.
char *z[1]; and char *name[]
Both name and z are not arrays of char. They are both an array of one pointer to char.
Both pointers name[1] and z[1] point to no valid memory, thus dereferencing it and attempting to store a string to that undefined memory by using strcpy(*name, "y"); and strcpy(*z, "z"); and invokes undefined behavior. - What you need is an array of char for both, name and z.
For storing a string you need one element to store the string-terminating null character.
Use char z[2] and name[2], if you only want to have strings contained of one single character.
BTW, Your code is a little bit complicate handled. You do not to use strings if you want to store and compare only single characters. It could be simplified as:
#include <stdio.h>
int main (void) {
char name = 'y';
printf("In name is the character: '%c'\n", name);
printf("Is the character 'z' in 'name'? %d", name == 'z');
return 0;
}
Output:
In name is the character: 'y'
Is the character 'z' in 'name'? 0
Where 0 means false and 1 signifies true.
Side Note:
You didnĀ“t need to #include <stdlib.h> at any point of time.

C passing array of pointers

This relates to C. I am having some trouble understanding how I can assign strings to char pointers within arrays from a function.
#include <stdio.h>
#include <string.h>
void function(char* array[]);
int main(void)
{
char* array[50];
function(array);
printf("array string 0: %s\n",array[0]);
printf("array string 1: %s\n",array[1]);
}
void function(char* array[])
{
char temp[] = "hello";
array[0] = temp;
array[1] = temp;
return;
}
Ideally, I would like the main printf function to return
array string 0: hello
array string 1: hello
But I'm having trouble understanding arrays of pointers, how these pass to functions and how to manipulate them in the function. If I declare a string like char temp[] = "string" then how do I assign this to one of the main function array[i] pointers? (assuming I have my jargon right)
char temp[] = "hello"; only creates a local, temporary array inside the function. So when the function exists, the array will be destroyed.
But with array[0] = temp; you're making array[0] point to the local array temp.
After the function returns, temp doesn't exist anymore. So accessing array[0] which pointed to temp will cause undefined behavior.
You could simply make temp static, so it also exists outside the function:
static char temp[] = "hello";
Or, you could copy the "hello" string to array[0] and array[1]. For copying C-strings, you normally use strcpy.
char temp[] = "hello";
strcpy(array[0], temp);
strcpy(array[1], temp);
However, before copying you need to make sure array[0] and array[1] point to memory that has enough space to hold all characters of "hello", including the terminating null character. So you have to do something like this before calling strcpy:
array[0] = malloc(6);
array[1] = malloc(6);
(6 is the minimum numbers of characters that can hold "hello".)
how do I assign this to one of the main function array[i] pointers
Arrays cannot be assigned.
A pointer cannot hold an array, it can only refer to an array. For the latter the pointer needs to get an array's address assigned.
Referring 1.
This
char temp[] = "hello";
isn't an assigment, but an initialisation.
This
char temp[];
temp[] = "hello";
would not compile (the 2nd line errors), as an array cannot be assigned.
Referring 2.
This
char* array[50];
defines an array of 50 pointers to char, it could reference 50 char-arrays, that is 50 C-"strings". It cannot hold the C-"strings" themselfs.
Example
Applying what is mentioned above to your code whould lead to for example the following:
#include <stdio.h>
#include <string.h>
void function(char* array[]);
int main(void)
{
char* array[50];
/* Make the 1st two elements point to valid memory. */
array[0] = malloc(42); /* Real code shall test the outcome of malloc()
as it might fail and very well return NULL! */
array[1] = malloc(42);
function(array);
printf("array string 0: %s\n",array[0]);
printf("array string 1: %s\n",array[1]);
return 0;
}
void function(char* array[])
{
char temp[] = "hello";
/* Copy the content of temp into the memory that was allocated to
the array's 1st memebrs in main(). */
strcpy(array[0], temp);
strcpy(array[1], temp);
return;
}
first, you need to allocate the destination.
second, char temp[] = "hello"; in function() is local variable. you cannot use their outside of the function. use strcpy to copy the content.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char* dest)
{
char temp[] = "hello";
strcpy(dest, temp);
return;
}
int main(void)
{
// or just char dest[10] = {0};
char *dest = malloc(10);
function(dest);
printf("dest: %s\n", dest);
}
In you program, you defined char* array[50];, so you need to create memory space for each item:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char* a[])
{
char temp[] = "hello";
strcpy(a[0], temp);
strcpy(a[1], temp);
return;
}
int main(void)
{
char *a[50];
int i = 0;
for (i = 0; i < 50; ++i)
a[i] = malloc(10);
function(a);
printf("a[0]: %s\n", a[0]);
printf("a[1]: %s\n", a[1]);
}

Printing a string character-by-character

int main() {
int i;
char a[]={"Hello"};
while(a!='\0') {
printf("%c",*a);
a++;
}
getch();
return 0;
}
Strings are stored in contiguous memory locations & while passing the address to printf() it should print the character. I have jst started learning C. I am not able to find an answer to this. Pls help.
Well a is the name of the array which you cannot increment. It is illegal to change the address of the array.
So define a pointer to a and then increment
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
char a[]="Hello";
char *ptr = a;
while(*ptr!='\0')
{
printf("%c",*ptr);
// a++ here would be illegal
ptr++;
}
getch();
return 0;
}
NOTE:
In fact, arrays in C are non-modifiable lvalues. There are no
operations in C that can modify the array itself (only individual
elements can be modifiable).
In your code, a is a name of an array, you can't modify it like a++. Use a pointer like this:
char *p = "Hello";
while(*p++)
{
printf("%c",*p);
}
Three problems:
char a[]={"Hello"}; is illegal. {"Hello"} can only initialize char* a[]. You probably want char a[]="Hello";
while(a!='\0') - you probably meant *a != '\0'. a is the array itself.
a++; - an array cannot be incremented. you should increment a pointer pointing to it.
You can also try it using a for loop:
#include <stdio.h>
int main(void) {
char a[] = "Hello";
char *p;
for(p = a; *p != '\0'; p++) {
printf("%c", *p);
}
return 0;
}

Function makes an array empty

I have the following code:
#include <stdio.h>
void insertion_sort(char[], int);
void swap(char*, char*);
int main() {
char s[] = "hello world";
puts(s);
insertion_sort(s, sizeof(s)/sizeof(char));
puts("done\n");
puts(s);
return 0;
}
void swap(char* a, char* b) {
char tmp = *a;
*a = *b;
*b = tmp;
}
void insertion_sort(char s[], int n)
{
int i,j;
/* counters */
for (i=1; i<n; i++) {
j=i;
while ((j>0) && (s[j] < s[j-1])) {
swap(&s[j],&s[j-1]);
j = j-1;
}
printf("%s\n", s);
}
}
The problem is, after the insertion_sort() function call, s becomes empty - puts(s) prints nothing.
Please advise.
Change:
insertion_sort(s, sizeof(s)/sizeof(char));
to:
insertion_sort(s, strlen(s));
otherwise you will be including the '\0' terminator of s[] in your sort.
Note that you will need an additional header for strlen so change:
#include <stdio.h>
to:
#include <stdio.h> // printf etc
#include <string.h> // strlen etc
The problem is that the length that you pass to insertion_sort includes terminating \0 character, which happens to have value 0, so in sort it is placed as the first element of your array. This is why your last puts() prints nothing - because the first character is now "the end of a string".
I suggest you to calculate the size of a string using strlen() which will return the length of a string excluding terminating character. Or if you want to do it your way, take terminating character into consideration and substract it from the total length.

Resources