473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointer to char

15 New Member
Hello,

i have the following code:

typedef struct
{
char *matrikelnr;
}student;


int main (void)
{

student *studall[2];
char tVal[10];

studall[0] = (student*)malloc(sizeof(student));
strcpy(tVal,"12345");
studall[0]->matrikelnr = tVal;
printf("studall[0]->matrikelnr: %s\n",studall[0]->matrikelnr);

studall[1] = (student*)malloc(sizeof(student));
strcpy(tVal,"12346");
studall[1]->matrikelnr = tVal;
printf("studall[1]->matrikelnr: %s\n",studall[1]->matrikelnr);

printf("studall[0]->matrikelnr: %s\n",studall[0]->matrikelnr);

return 0;
}

output:

studall[0]->matrikelnr: 12345
studall[1]->matrikelnr: 12346
studall[0]->matrikelnr: 12346

My Question: How can i get for stuall[0] the first value 12345? Could any one please help me.

thanks in advance.
Maryam
Feb 11 '08 #1
2 922
weaknessforcats
9,208 Recognized Expert Moderator Expert
studall[0]->matrikelnr = tVal;
Stop changing matrikelnr.

matrikelnr is a char pointer. tVal is also a char pointer. (Remember, the name of an array is the address of element 0 and tVal[0] is a char). In effect all of your array elements point to tVal so they all report the final value placed in tVal.

What you need to to is copy the tVal array to matrikelnr. That means you a) need to allocate memory for the copy and b) actually copy the array instead of just assigning its address.
Feb 11 '08 #2
Maryan
15 New Member
Thank you for answering my Question. I have done it like this:

typedef struct
{
char *matrikelnr;
}student;


int main (void)
{

student *studall[2];

char *tVal;
studall[0] = (student*)malloc(sizeof(student));

tVal=(char*)malloc(10);
strcpy(tVal,"12345");
studall[0]->matrikelnr = tVal;
printf("studall[0]->matrikelnr: %s\n",studall[0]->matrikelnr);

studall[1] = (student*)malloc(sizeof(student));

tVal=(char*)malloc(10);
strcpy(tVal,"12346");
studall[1]->matrikelnr = tVal;
printf("studall[1]->matrikelnr: %s\n",studall[1]->matrikelnr);

printf("studall[0]->matrikelnr: %s\n",studall[0]->matrikelnr);
return 0;
}

and it works.

bye,
Maryan
Feb 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.